Created
December 5, 2016 08:12
-
-
Save sujeetkv/9e9998fafdacfb069811f3bfb9f0311f to your computer and use it in GitHub Desktop.
Node.js mysql helper module for simple queries
This file contains 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
/* using connection pool (recommended) */ | |
var mysql = require('mysql'); | |
var log = require('./mylogger'); | |
var local_context = this; | |
var SimpleQueryException = function (message) { | |
this.name = 'SimpleQueryException'; | |
this.message = message; | |
}; | |
SimpleQueryException.prototype.toString = function () { | |
return this.name + ': ' + this.message; | |
}; | |
var configureSimpleQuery = function (db_config, context) { | |
context = context || local_context; | |
if ( ! ('connectionLimit' in db_config)) { | |
db_config.connectionLimit = 100; | |
} | |
var connection_pool = mysql.createPool(db_config); | |
/* end all connections on exit */ | |
//process.stdin.resume(); | |
process.on('exit', function (code) { | |
connection_pool.end(); | |
log('Database connection pool closed.'); | |
process.exit(code); | |
}); | |
/* catch Ctrl+C */ | |
process.on('SIGINT', function () { | |
process.exit(0); | |
}); | |
var conn_ids = []; | |
connection_pool.on('connection', function (connection) { | |
if (conn_ids.indexOf(connection.threadId) > -1) { | |
log('Database connection reused [connection-id: ' + connection.threadId + ']'); | |
} else { | |
conn_ids.push(connection.threadId); | |
log('Database connection created [connection-id: ' + connection.threadId + ']'); | |
} | |
}); | |
var simpleQuery = function () { | |
var query, options, callback; | |
if (arguments.length < 2) { | |
throw new SimpleQueryException('Atleast 2 parameters required.'); | |
} else if (arguments.length > 2) { | |
query = arguments[0]; | |
options = arguments[1]; | |
callback = arguments[2]; | |
} else { | |
query = arguments[0]; | |
callback = arguments[1]; | |
} | |
connection_pool.getConnection(function (err, connection) { | |
if (err) { | |
connection.release(); | |
callback.call(context, err, [], []); | |
return; | |
} | |
if (options) { | |
connection.query(query, options, function (err, results, fields) { | |
connection.release(); | |
callback.call(context, err, results, fields); | |
}); | |
} else { | |
connection.query(query, function (err, results, fields) { | |
connection.release(); | |
callback.call(context, err, results, fields); | |
}); | |
} | |
/*connection.on('error', function(err) { | |
callback.call(context, err, [], []); | |
return; | |
});*/ | |
}); | |
}; | |
return simpleQuery; | |
}; | |
module.exports = configureSimpleQuery; |
This file contains 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
/* using new connection per request */ | |
var mysql = require('mysql'); | |
var log = require('./mylogger'); | |
var local_context = this; | |
var SimpleQueryException = function (message) { | |
this.name = 'SimpleQueryException'; | |
this.message = message; | |
}; | |
SimpleQueryException.prototype.toString = function () { | |
return this.name + ': ' + this.message; | |
}; | |
var configureSimpleQuery = function (db_config, context) { | |
context = context || local_context; | |
var SimpleQuery = function () { | |
var query, options, callback; | |
if (arguments.length < 2) { | |
throw new SimpleQueryException('Atleast 2 parameters required.'); | |
} else if (arguments.length > 2) { | |
query = arguments[0]; | |
options = arguments[1]; | |
callback = arguments[2]; | |
} else { | |
query = arguments[0]; | |
callback = arguments[1]; | |
} | |
var db = mysql.createConnection(db_config); | |
db.connect(function (err) { | |
if (err) { | |
callback.call(context, err, [], []); | |
return; | |
} | |
log('Database connected! [connection-id: ' + db.threadId + ']'); | |
}); | |
if (options) { | |
db.query(query, options, function (err, results, fields) { | |
callback.call(context, err, results, fields); | |
}); | |
} else { | |
db.query(query, function (err, results, fields) { | |
callback.call(context, err, results, fields); | |
}); | |
} | |
db.on('error', function (err) { | |
callback.call(context, err, [], []); | |
}); | |
db.end(function (err) { | |
if (err) { | |
log().error('DB Error!', err); | |
return; | |
} | |
log('Database disconnected! [connection-id: ' + db.threadId + ']'); | |
}); | |
}; | |
return SimpleQuery; | |
}; | |
module.exports = configureSimpleQuery; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment