Permalink
Browse files

Update the PerRequestDb plugin to use the separate db plugin library …

…instead of it's own inline code.
  • Loading branch information...
1 parent 9cd912e commit 207861960d2bbed5bf4ed79994809d677e48b367 @scull7 scull7 committed Mar 6, 2012
Showing with 28 additions and 39 deletions.
  1. +28 −39 lib/plugins/PerRequestDb/PerRequestDb.js
@@ -1,40 +1,29 @@
-module.exports = function (MVC, server, driver_name, config_name) {
- var db_config = MVC.config.getValue(config_name || 'database'),
- db = require('./driver/' + (driver_name || db_config.driver || 'MySQL')),
- driver, pool;
-
- if (typeof db_config.password !== 'string') {
- db_config.password = '';
- }
-
- driver = db(db_config);
- pool = require('generic-pool').Pool({
- 'name': driver_name,
- 'max': 255,
- 'create': driver.create,
- 'destroy': driver.destroy
- });
-
- return function (req, res, next) {
- if (!req.db) {
- pool.acquire(function (error, db) {
- if (error) {
- return res.end('CONNECTION ERROR: ' + error);
- } else {
- req.db = db;
-
- //Make sure we release the connection on response end.
- var end = res.end;
-
- res.end = function () {
- pool.release(db);
- end.apply(this, arguments);
- };
- return next();
- }
- });
- } else {
- next();
- }
- };
+module.exports = function(MVC, server, driver_name, config_name) {
+ var db_config = MVC.config.getValue(config_name || 'database'),
+ pool = MVC.Library('db')(db_config, driver_name);
+
+ return function(req, res, next) {
+ if (!req.db) {
+ pool.acquire(function(error, db) {
+ if (error) {
+ return res.end('CONNECTION ERROR: ' + error);
+ }
+ else {
+ req.db = db;
+
+ //Make sure we release the connection on response end.
+ var end = res.end;
+
+ res.end = function() {
+ pool.release(db);
+ end.apply(this, arguments);
+ };
+ return next();
+ }
+ });
+ }
+ else {
+ next();
+ }
+ };
};

0 comments on commit 2078619

Please sign in to comment.