Skip to content
Browse files

Version 1.0

  • Loading branch information...
1 parent 56b4280 commit e258015f09337a4c318889c9bbcf41359afe121a @jhurliman committed Jun 19, 2011
Showing with 171 additions and 0 deletions.
  1. +20 −0 LICENSE.txt
  2. +1 −0 index.js
  3. +135 −0 lib/database.js
  4. +15 −0 package.json
View
20 LICENSE.txt
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Cull TV, Inc. <jhurliman@cull.tv>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/database');
View
135 lib/database.js
@@ -0,0 +1,135 @@
+
+var poolModule = require('generic-pool');
+var mysqlClient = require('mysql').Client;
+
+var pool = null;
+
+/**
+ * Initialize the MySQL connection pool with the given database details.
+ */
+exports.init = function(dbUser, dbPass, dbDatabase, dbHost, dbPort) {
+ pool = poolModule.Pool({
+ name : 'mysql',
+ create : function(callback) {
+ var c = new mysqlClient();
+ if (dbUser) c.user = dbUser;
+ if (dbPass) c.password = dbPass;
+ if (dbDatabase) c.database = dbDatabase;
+ if (dbHost) c.host = dbHost;
+ if (dbPort) c.port = dbPort;
+
+ callback(c);
+ },
+ destroy : function(client) {
+ if (client.connected) {
+ try { client.end(); }
+ catch (err) { console.error('Failed to close MySQL connection: ' + err); }
+ }
+ },
+ max : 50,
+ idleTimeoutMillis : 30000,
+ log: false
+ });
+};
+
+/**
+ * Execute a query that is expected to return zero or more rows.
+ * @param {string} query SQL query to execute
+ * @param {Array.<Object>} data Parameters to substitute into the query
+ * @param {function(string, Array.<Object>)} callback Callback to execute when
+ * the query completes
+ */
+exports.query = function(query, data, callback) {
+ _doDatabase(function(err, client) {
+ if (err) {
+ callback(err, null);
+ return pool.release(client);
+ }
+
+ client.query(query, data, function(err, results, fields) {
+ callback(err, results);
+ pool.release(client);
+ });
+ });
+};
+
+/**
+ * Execute a query that is expected to return zero or one rows.
+ */
+exports.querySingle = function(query, data, callback) {
+ _doDatabase(function(err, client) {
+ if (err) {
+ callback(err, null);
+ return pool.release(client);
+ }
+
+ client.query(query, data, function(err, results, fields) {
+ if (!err && results && results.length > 0) {
+ callback(null, results[0]);
+ } else {
+ callback(err, null);
+ }
+
+ pool.release(client);
+ });
+ });
+};
+
+/**
+ * Execute a query that is expected to return many rows, and stream the results
+ * back one row at a time.
+ */
+exports.queryMany = function(query, data, rowCallback, endCallback) {
+ _doDatabase(function(err, client) {
+ if (err) {
+ if (endCallback) endCallback(err);
+ return pool.release(client);
+ }
+
+ client.query(query, data)
+ .on('error', function(err) {
+ if (endCallback) endCallback(err);
+ pool.release(client);
+ })
+ .on('row', rowCallback)
+ .on('end', function() {
+ if (endCallback) endCallback(null);
+ pool.release(client);
+ });
+ });
+};
+
+/**
+ * Execute a query that is not expected to return any rows.
+ */
+exports.nonQuery = function(query, data, callback) {
+ _doDatabase(function(err, client) {
+ if (err) {
+ if (callback) callback(err, null);
+ return pool.release(client);
+ }
+
+ client.query(query, data, function(err, info) {
+ if (callback) callback(err, info);
+ pool.release(client);
+ });
+ });
+};
+
+/**
+ * Handles the acquisition of a client from the pool and connecting to the
+ * database. Does not handle releasing the client back to the pool.
+ * @param {function(string, Object)} callback
+ */
+function _doDatabase(callback)
+{
+ pool.acquire(function(client) {
+ if (client.connected) {
+ callback(null, client);
+ } else {
+ client.connect(function(err) {
+ callback(err, client);
+ });
+ }
+ });
+}
View
15 package.json
@@ -0,0 +1,15 @@
+{
+ "name": "mysql-simple",
+ "description": "Provides connection pooling and a simplified interface on top of node-mysql and generic-pool.",
+ "version": "1.0.0",
+ "author": "John Hurliman <jhurliman@cull.tv>",
+ "contributors": [],
+ "dependencies": {
+ "mysql": ">= 0.9.1",
+ "generic-pool": ">= 1.0.6"
+ },
+ "keywords": ["mysql", "database", "pooling", "native"],
+ "directories": { "lib": "./lib" },
+ "main": "index",
+ "engines": { "node": "*" }
+}

0 comments on commit e258015

Please sign in to comment.
Something went wrong with that request. Please try again.