Created
September 29, 2014 09:11
-
-
Save you21979/3bb7a82d3bffd285bf23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require("mysql"); | |
var Promise = require("bluebird"); | |
Promise.promisifyAll(mysql); | |
Promise.promisifyAll(require("mysql/lib/Connection").prototype); | |
Promise.promisifyAll(require("mysql/lib/Pool").prototype); | |
var getConnection = exports.getConnection = function() { | |
return pool.getConnectionAsync().disposer(function(connection) { | |
try { | |
connection.release(); | |
} catch(e) {}; | |
}); | |
} | |
var pool; | |
var initialize = exports.initialize = function(opt){ | |
pool = mysql.createPool(opt); | |
} | |
var finalize = exports.finalize = function(){ | |
pool.end(); | |
} | |
initialize({ | |
connectionLimit: 10, | |
host: 'localhost', | |
user: 'root', | |
password: '', | |
database:'mysql' | |
}); | |
var using = Promise.using; | |
using(getConnection(), function(connection) { | |
return connection.queryAsync("SELECT * FROM user"); | |
}).then(function(rows) { | |
console.log(rows); | |
finalize(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment