This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
node-task/node_modules/express-myconnection
Timothy Warren 41a7f7e20d Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
..
examples/single Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
lib Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
.npmignore Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
LICENSE Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
README.md Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
index.js Add bcrypt and session modules 2014-09-23 14:54:05 -04:00
package.json Add bcrypt and session modules 2014-09-23 14:54:05 -04:00

README.md

express-myconnection

Connect/Express middleware provides a consistent API for MySQL connections during request/response life cycle. It supports three different strategies of managing db connections: single for a singleton connection on an app instance level, pool based connections, and a new connection per each request. Its also capable of auto closing/releasing connections if configured either with pool or request. It uses node-mysql as a MySQL driver.

Strategies

  • single - creates single database connection for an application instance. Connection is never closed. In case of disconnection it will try to reconnect again as described in node-mysql docs.
  • pool - creates pool of connections on an app instance level, and serves a single connection from pool per request. The connections is auto released to the pool at the response end.
  • request - creates new connection per each request, and automatically closes it at the response end.

Usage

Configuration is straightforward and you use it as any other middleware. First param it accepts is a node-mysql module, second is a db options hash passed to node-mysql module when connection or pool are created. The third is string defining strategy type.

// app.js
...
var mysql = require('mysql'), // node-mysql module
    myConnection = require('express-myconnection'), // express-myconnection module
    dbOptions = {
      host: 'localhost',
      user: 'dbuser',
      password: 'password',
      port: 3306,
      database: 'mydb'
    };
  
app.use(myConnection(mysql, dbOptions, 'single');
...

express-myconnection extends request object with getConection(callback) function, this way connection instance can be accessed anywhere in routers during request/response life cycle:

// myroute.js
...
module.exports = function(req, res, next) {
    ...
    req.getConnection(function(err, connection) {
      if (err) return next(err);
      
      connection.query('SELECT 1 AS RESULT', [], function(err, results) {
        if (err) return next(err);
        
        results[0].RESULT;
        // -> 1
        
        res.send(200);
      });
      
    });
    ...
}
...