mysql-connection-pool

A node.js mysql adapter for managing/querying pooled connections on top of `node-mysql` module.

npm install mysql-connection-pool
1 downloads in the last week
4 downloads in the last month

Install

npm install mysql-connection-pool

Introduction

A Node.js api wrapper for a cluster of MySQL instances. Probably a naive approach but could become more fully-featured and tested in the future. Would only recommend as experimental. Uses node-mysql and underscore modules.

Here's an example using it:

var configurations = [
    {host:'0.0.0.0', user:'user', password:'secret'},
    {host:'0.0.0.1', user:'user', password:'secret'},
    {host:'0.0.0.2', user:'user', password:'secret'}
];

// Initialize pool of connections
var pool = new MysqlConnectionPool(configurations);

// Opens all connections in the pool
pool.open();

// Query a connection in the pool, logging output
pool.query('show tables;', function(err, rows){
    if (err){ throw err; }
    console.log(rows);
});

// Close the entire pool after 3 seconds (otherwise thread remains open)
setTimeout(function(){pool.close();}, 3000);

Using an external config:

var _ = require('underscore');
var config = require('./config');
var MysqlConnectionPool = require('./index');

var pool = new MysqlConnectionPool( _.map(config.hosts, function(host){
    return {
        host     : host,
        user     : config.user,
        password : config.password,
        database : config.database
    };
}));
npm loves you