不要怂,就是干,撸起袖子干!

Commit 18cecf6c by Sascha Depold

node-mysql 0.9.4 support

1 parent 3523e365
var Utils = require("./utils")
var Query = module.exports = function(databaseConfig, callee, options) {
this.config = databaseConfig
var Query = module.exports = function(client, callee, options) {
var self = this
this.client = client
this.callee = callee
this.options = options || {}
this.client.on('error', function(err) { self.onFailure(err) })
}
Utils.addEventEmitter(Query)
Query.prototype.run = function(query) {
var self = this
var client = new (require("mysql").Client)({
user: this.config.username,
password: this.config.password,
host: this.config.host,
port: this.config.port,
database: this.config.database
})
this.sql = query
if(this.options.logging)
console.log('Executing: ' + this.sql)
client.connect()
client.query(this.sql, function(err, results, fields) {
this.client.query(this.sql, function(err, results, fields) {
err ? self.onFailure(err) : self.onSuccess(self.sql, results, fields)
})
client.on('error', function(err) { self.onFailure(err) })
client.end()
}).setMaxListeners(100)
return this
}
......
......@@ -19,10 +19,43 @@ var Sequelize = module.exports = function(database, username, password, options)
host : options.host || 'localhost',
port : options.port || 3306
}
this.connectorCheckTimeoutId = null
}
Sequelize.Utils = Utils
var instanceMethods = {
initConnector: function() {
var self = this
this.client = require("mysql").createClient({
user: this.config.username,
password: this.config.password,
host: this.config.host,
port: this.config.port,
database: this.config.database
})
this.client.setMaxListeners(100)
},
freeConnector: function() {
var self = this
this.client.end(function() {
self.client = null
self.connectorCheckTimeoutId = null
})
},
checkConnector: function() {
var self = this
this.connectorCheckTimeoutId && clearTimeout(this.connectorCheckTimeoutId)
this.connectorCheckTimeoutId = setTimeout(function() {
if(self.client && self.client._queue && (self.client._queue.length === 0))
self.freeConnector()
}, 500)
},
define: function(modelName, attributes, options) {
options = options || {}
......@@ -42,9 +75,18 @@ var instanceMethods = {
options = options || {}
if(this.options.queryOptions) options = Sequelize.Utils.merge(options, this.options.queryOptions)
if(!this.client) this.initConnector()
options.logging = this.options.hasOwnProperty('logging') ? this.options.logging : true
return new Query(this.config, callee, options).run(sql)
var self = this
, query = new Query(this.client, callee, options).run(sql)
query
.on('success', function(){ self.checkConnector() })
.on('failure', function(){ self.checkConnector() })
return query
},
sync: function(options) {
......
......@@ -7,7 +7,7 @@
{ "name": "Sascha Depold", "email": "sascha@depold.com" }
],
"dependencies": {
"mysql": ">=0.9.1",
"mysql": ">=0.9.4",
"underscore": ">=1.1.5",
"underscore.string": ">=1.1.3",
"lingo": ">=0.0.4"
......
......@@ -11,7 +11,9 @@ module.exports = {
'sync should fail with incorrect database config': function(exit) {
var s = new Sequelize('foo', 'bar', null, {logging: false})
var User2 = s.define('User', { name: Sequelize.STRING, bio: Sequelize.TEXT })
User2.sync().on('failure', function(){exit(function(){})})
User2.sync().on('failure', function(err){
exit(function(){}
)})
},
'drop should work': function(exit) {
User.drop().on('success', function(){exit(function(){})})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!