express-mysql-connection
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -7,3 +7,2 @@ var _mysql, | ||
module.exports = function (mysql, dbConfig, strategy) { | ||
@@ -27,3 +26,2 @@ | ||
handleDisconnect(dbConfig); | ||
// single connection strategy | ||
// 单链接策略时返回的中间件 * * * * * * * * * * * * * * * * | ||
@@ -41,11 +39,9 @@ _middlewareFn = function(req, res, next) { | ||
_pool = _mysql.createPool(dbConfig); | ||
// pool | ||
// 连接池策略时返回的中间件 * * * * * * * * * * * * * * * * | ||
_middlewareFn = function(req, res, next) { | ||
var poolConn = null; | ||
// Returning cached connection from a pool, caching is on request level | ||
if(req.__expressMysqlConnectionCache__) { | ||
req.getConnection = function (callback) { | ||
pollConn = req.__expressMysqlConnectionCache__; | ||
callback(null, requestConn); | ||
callback(null, req.__expressMysqlConnectionCache__); | ||
closeConnection(res, req.__expressMysqlConnectionCache__); | ||
} | ||
@@ -55,10 +51,10 @@ }else { | ||
req.getConnection = function (callback) { | ||
_pool.getConnection(function (err, connection) { | ||
_pool.getConnection(function (err, poolConn) { | ||
if (err) return callback(err); | ||
poolConnection = connection; | ||
callback(null, poolConnection); | ||
req.__expressMysqlConnectionCache__ = poolConn; | ||
callback(null, poolConn); | ||
closeConnection(res, poolConn); | ||
}); | ||
} | ||
} | ||
closeConnection(res, poolConn); | ||
next(); | ||
@@ -68,15 +64,13 @@ } | ||
case 'request': | ||
// request | ||
// 请求时创建连接,请求结束后自动释放 * * * * * * * * * * * * | ||
_middlewareFn = function(req, res, next) { | ||
var requestConn = null; | ||
// Returning cached connection, caching is on request level | ||
if(req.__expressMysqlConnectionCache__) { | ||
req.getConnection = function(callback) { | ||
requestConn = req.__expressMysqlConnectionCache__; | ||
callback(null, requestConn); | ||
callback(null, req.__expressMysqlConnectionCache__); | ||
} | ||
closeConnection(res, null, req.__expressMysqlConnectionCache__); | ||
}else { | ||
req.getConnection = function(callback) { | ||
requestConn = _mysql.createConnection(dbConfig); | ||
var requestConn = _mysql.createConnection(dbConfig); | ||
requestConn.connect(function (err) { | ||
@@ -87,5 +81,5 @@ if (err) return callback(err); | ||
}); | ||
closeConnection(res, null, requestConn); | ||
} | ||
} | ||
closeConnection(res, null, requestConn); | ||
next(); | ||
@@ -122,10 +116,14 @@ }; | ||
// Request closed unexpectedly. | ||
res.on("close", closeHandler); | ||
res.on("close", closeHandler); | ||
// Finish | ||
res.on("finish", closeHandler); | ||
res.on("finish", closeHandler); | ||
function closeHandler() { | ||
if (poolConnection) poolConnection.release(); | ||
if (requestConnection) requestConnection.end(); | ||
if (poolConnection) { | ||
poolConnection.release(); | ||
} | ||
if (requestConnection) { | ||
requestConnection.end(); | ||
} | ||
} | ||
} |
{ | ||
"name": "express-mysql-connection", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "express-mysql-connection middleware", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7248
111