Skip to content

Instantly share code, notes, and snippets.

@imposibrus
Created July 28, 2014 19:15
Show Gist options
  • Save imposibrus/056e7db82eb0598e54e6 to your computer and use it in GitHub Desktop.
Save imposibrus/056e7db82eb0598e54e6 to your computer and use it in GitHub Desktop.
Node.js MySQL connection pool with node-mysql package
var mysql = require('mysql');
function handleDisconnect(db_config) {
var connection = mysql.createPool(db_config);
connection.on('error', function(err) {
console.error('db error', err, err.code);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect(db_config);
} else {
throw err;
}
});
return connection;
}
var connection = handleDisconnect({
connectionLimit : 10,
host: 'localhost',
user: 'root',
password: '',
database: 'test',
dateStrings: true,
multipleStatements: true,
queryFormat: function (query, values) {
if (!values) return query;
return query.replace(/\:(\w+)/g, function (txt, key) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
}
});
module.exports = connection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment