mysql-activerecord
Advanced tools
Comparing version 0.8.3 to 0.8.4
156
index.js
@@ -31,31 +31,45 @@ /** | ||
exports.Adapter = function(settings) { | ||
var Adapter = function(settings) { | ||
var mysql = require('mysql'); | ||
if(settings.server) { | ||
settings.host = settings.server; | ||
var initializeConnectionSettings = function () { | ||
if(settings.server) { | ||
settings.host = settings.server; | ||
} | ||
if(settings.username) { | ||
settings.user = settings.username; | ||
} | ||
if (!settings.host) { | ||
throw new Error('Unable to start ActiveRecord - no server given.'); | ||
} | ||
if (!settings.port) { | ||
settings.port = 3306; | ||
} | ||
if (!settings.user) { | ||
settings.user = ''; | ||
} | ||
if (!settings.password) { | ||
settings.password = ''; | ||
} | ||
if (!settings.database) { | ||
throw new Error('Unable to start ActiveRecord - no database given.'); | ||
} | ||
return settings; | ||
}; | ||
var connection; | ||
var connectionSettings; | ||
var pool; | ||
if (settings && settings.pool) { | ||
pool = settings.pool.pool; | ||
connection = settings.pool.connection; | ||
} else { | ||
connectionSettings = initializeConnectionSettings(); | ||
connection = new mysql.createConnection(connectionSettings); | ||
} | ||
if(settings.username) { | ||
settings.user = settings.username; | ||
} | ||
if (!settings.host) { | ||
throw new Error('Unable to start ActiveRecord - no server given.'); | ||
} | ||
if (!settings.port) { | ||
settings.port = 3306; | ||
} | ||
if (!settings.user) { | ||
settings.user = ''; | ||
} | ||
if (!settings.password) { | ||
settings.password = ''; | ||
} | ||
if (!settings.database) { | ||
throw new Error('Unable to start ActiveRecord - no database given.'); | ||
} | ||
var connection = new mysql.createConnection(settings); | ||
if (settings.charset) { | ||
@@ -183,3 +197,4 @@ connection.query('SET NAMES ' + settings.charset); | ||
this.connection = function() { return connection; } | ||
this.connectionSettings = function() { return connectionSettings; }; | ||
this.connection = function() { return connection; }; | ||
@@ -215,3 +230,3 @@ this.where = function(whereSet, whereValue, isRaw) { | ||
responseCallback(err, null); | ||
else | ||
else | ||
responseCallback(null, res[0]['count']); | ||
@@ -223,3 +238,3 @@ }); | ||
return that; | ||
} | ||
}; | ||
@@ -362,3 +377,3 @@ this.join = function(tableName, relation, direction) { | ||
return that; | ||
} | ||
}; | ||
@@ -432,3 +447,11 @@ this.get = function(tableName, responseCallback) { | ||
}; | ||
this.releaseConnection = function() { | ||
pool.releaseConnection(connection); | ||
}; | ||
this.releaseConnection = function() { | ||
pool.releaseConnection(connection); | ||
}; | ||
var reconnectingTimeout = false; | ||
@@ -454,3 +477,5 @@ | ||
handleDisconnect(connection); | ||
if (!pool) { | ||
handleDisconnect(connection); | ||
} | ||
@@ -460,2 +485,67 @@ var that = this; | ||
return this; | ||
} | ||
}; | ||
var mysqlPool; // this should be initialized only once. | ||
var mysqlCharset; | ||
var Pool = function (settings) { | ||
if (!mysqlPool) { | ||
var mysql = require('mysql'); | ||
var poolOption = { | ||
createConnection: settings.createConnection, | ||
waitForConnections: settings.waitForConnections, | ||
connectionLimit: settings.connectionLimit, | ||
queueLimit: settings.queueLimit | ||
}; | ||
Object.keys(poolOption).forEach(function (element) { | ||
// Avoid pool option being used by mysql connection. | ||
delete settings[element]; | ||
// Also remove undefined elements from poolOption | ||
if (!poolOption[element]) { | ||
delete poolOption[element]; | ||
} | ||
}); | ||
// Confirm settings with Adapter. | ||
var db = new Adapter(settings); | ||
var connectionSettings = db.connectionSettings(); | ||
Object.keys(connectionSettings).forEach(function (element) { | ||
poolOption[element] = connectionSettings[element]; | ||
}); | ||
mysqlPool = mysql.createPool(poolOption); | ||
mysqlCharset = settings.charset; | ||
} | ||
this.pool = function () { | ||
return mysqlPool; | ||
}; | ||
this.getNewAdapter = function (responseCallback) { | ||
mysqlPool.getConnection(function (err, connection) { | ||
if (err) { | ||
throw err; | ||
} | ||
var adapter = new Adapter({ | ||
pool: { | ||
pool: mysqlPool, | ||
enabled: true, | ||
connection: connection | ||
}, | ||
charset: mysqlCharset | ||
}); | ||
responseCallback(adapter); | ||
}); | ||
}; | ||
this.disconnect = function (responseCallback) { | ||
this.pool().end(responseCallback); | ||
}; | ||
return this; | ||
}; | ||
exports.Adapter = Adapter; | ||
exports.Pool = Pool; |
{ | ||
"name" : "mysql-activerecord", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"author": "Martin Tajur <martin@tajur.ee>", | ||
@@ -11,5 +11,9 @@ "description": "MySQL ActiveRecord pattern implementation on top of the mysql module.", | ||
}, | ||
"contributors": ["Daniel Bretoi <daniel@bretoi.com>"], | ||
"contributors": [ | ||
"Daniel Bretoi <daniel@bretoi.com>", | ||
"Kyle Farris <kyle@chomponllc.com>", | ||
"Daehyub Kim <lateau@gmail.com>" | ||
], | ||
"dependencies": { | ||
"mysql": "2.0.0-alpha8" | ||
"mysql": "~2.1" | ||
}, | ||
@@ -16,0 +20,0 @@ "main" : "./", |
@@ -229,3 +229,26 @@ MySQL ActiveRecord Adapter for Node.js | ||
Pooling connections | ||
=================== | ||
Single or multiple connections can be pooled with the Pool object. | ||
var Db = require('mysql-activerecord'); | ||
var pool = new Db.Pool({ | ||
server: 'localhost', | ||
username: 'root', | ||
password: '12345', | ||
database: 'test' | ||
}); | ||
pool.getNewAdapter(function(db) { | ||
db | ||
.where({ name: 'Martin' }) | ||
.get('people', function(err, results, fields) { | ||
console.log(results); | ||
db.releaseConnection(); | ||
// do not do anything with db that has been released. | ||
}); | ||
}); | ||
Some more usage examples | ||
@@ -238,8 +261,8 @@ ======================== | ||
var Db = require('mysql-activerecord'); | ||
var db = new Db.Adapter({ | ||
server: 'localhost', | ||
username: 'root', | ||
password: '12345', | ||
database: 'test' | ||
}); | ||
var db = new Db.Adapter({ | ||
server: 'localhost', | ||
username: 'root', | ||
password: '12345', | ||
database: 'test' | ||
}); | ||
@@ -246,0 +269,0 @@ Basic SELECT query |
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
26950
462
391
+ Addedcore-util-is@1.0.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedmysql@2.1.1(transitive)
+ Addedreadable-stream@1.1.14(transitive)
+ Addedstring_decoder@0.10.31(transitive)
- Removedmysql@2.0.0-alpha8(transitive)
Updatedmysql@~2.1