mysql-pool.js 533 B

123456789101112131415161718192021
  1. /**
  2. */
  3. var Mysql = require('mysql');
  4. var pool;
  5. var client = {
  6. getConnection:function(callback){
  7. if(!pool) throw new Error("Mysql pool not created!");
  8. if(!callback || typeof(callback)!='function') throw new Error("Mysql pool get connection lost callback!");
  9. pool.getConnection(function(err,connection){
  10. callback(err,connection);
  11. });
  12. },
  13. end:function(){
  14. pool.end();
  15. }
  16. }
  17. exports.instance = function(config){
  18. pool = Mysql.createPool(config)
  19. return client;
  20. };