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

Commit 12fd2280 by Sascha Depold

refactored instance methods of sequelize

1 parent c29500a3
Showing with 45 additions and 48 deletions
...@@ -25,71 +25,68 @@ var Sequelize = module.exports = function(database, username, password, options) ...@@ -25,71 +25,68 @@ var Sequelize = module.exports = function(database, username, password, options)
this.modelManager = new ModelManager(this) this.modelManager = new ModelManager(this)
this.connectorManager = new ConnectorManager(this, this.config) this.connectorManager = new ConnectorManager(this, this.config)
} }
Sequelize.Utils = Utils Sequelize.Utils = Utils
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
var instanceMethods = { Sequelize.prototype.define = function(modelName, attributes, options) {
define: function(modelName, attributes, options) { options = options || {}
options = options || {}
if(this.options.define) if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define) options = Sequelize.Utils.merge(options, this.options.define)
var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options)) var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options))
return model return model
}, }
import: function(path) { Sequelize.prototype.import = function(path) {
var defineCall = require(path) var defineCall = require(path)
return defineCall(this, DataTypes) return defineCall(this, DataTypes)
}, }
query: function(sql, callee, options) { Sequelize.prototype.query = function(sql, callee, options) {
options = options || {} options = options || {}
if(this.options.query) if(this.options.query)
options = Sequelize.Utils.merge(options, this.options.query) options = Sequelize.Utils.merge(options, this.options.query)
options.logging = this.options.hasOwnProperty('logging') ? this.options.logging : true options.logging = this.options.hasOwnProperty('logging') ? this.options.logging : true
return this.connectorManager.query(sql, callee, options) return this.connectorManager.query(sql, callee, options)
}, }
sync: function(options) { Sequelize.prototype.sync = function(options) {
options = options || {} options = options || {}
if(this.options.sync) if(this.options.sync)
options = Sequelize.Utils.merge(options, this.options.sync) options = Sequelize.Utils.merge(options, this.options.sync)
var self = this var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() { var eventEmitter = new Utils.CustomEventEmitter(function() {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.sync(options)) }) self.modelManager.models.forEach(function(model) { chainer.add(model.sync(options)) })
chainer chainer
.run() .run()
.on('success', function() { eventEmitter.emit('success', null) }) .on('success', function() { eventEmitter.emit('success', null) })
.on('failure', function(err) { eventEmitter.emit('failure', err) }) .on('failure', function(err) { eventEmitter.emit('failure', err) })
}) })
return eventEmitter.run() return eventEmitter.run()
}, }
drop: function() { Sequelize.prototype.drop = function() {
var self = this var self = this
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.drop()) }) self.modelManager.models.forEach(function(model) { chainer.add(model.drop()) })
chainer chainer
.run() .run()
.on('success', function() { emitter.emit('success', null) }) .on('success', function() { emitter.emit('success', null) })
.on('failure', function(err) { emitter.emit('failure', err) }) .on('failure', function(err) { emitter.emit('failure', err) })
}).run() }).run()
}
} }
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.Utils._.map(instanceMethods, function(fct, name) { Sequelize.prototype[name] = fct})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!