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

Commit eb305c3f by sdepold

min, insert, update via query interface

1 parent 1c7e685b
......@@ -81,14 +81,10 @@ module.exports = (function() {
return this.QueryInterface.rawSelect(this.tableName, options, 'max')
}
ModelFactory.prototype.min = function(field, options) {
var self = this
options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['min(' + field + ')', 'min'])
var emitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).success(function(obj) {
emitter.emit('success', obj['min'])
})
})
return emitter.run()
return this.QueryInterface.rawSelect(this.tableName, options, 'min')
}
ModelFactory.prototype.findAll = function(options) {
......@@ -96,6 +92,11 @@ module.exports = (function() {
}
ModelFactory.prototype.find = function(options) {
if([null, undefined].indexOf(options) > -1) {
var NullEmitter = require("./emitters/null-emitter")
return new NullEmitter()
}
// options is not a hash but an id
if(typeof options == 'number')
options = {where: options}
......@@ -109,15 +110,11 @@ module.exports = (function() {
})
options = {where: where}
} else if((options == null) || (options == undefined)) {
var NullEmitter = require("./emitters/null-emitter")
return new NullEmitter()
}
options.limit = 1
var sql = this.QueryGenerator.selectQuery(this.tableName, options)
return query.call(this, sql, this, {plain: true})
return this.QueryInterface.select(this, this.tableName, options, {plain: true})
}
ModelFactory.prototype.build = function(values, options) {
......
......@@ -32,7 +32,7 @@ module.exports = (function() {
})
Model.prototype.__defineGetter__('QueryInterface', function() {
return this.__definition.modelManager.QueryInterface
return this.__definition.modelManager.sequelize.getQueryInterface()
})
Model.prototype.save = function() {
......@@ -42,19 +42,10 @@ module.exports = (function() {
this[attr] = new Date()
if(this.isNewRecord) {
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.insertQuery(self.__definition.tableName, self.values))
.success(function(obj) {
obj.isNewRecord = false
eventEmitter.emit('success', obj)
})
.error(function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
return this.QueryInterface.insert(this, this.__definition.tableName, this.values)
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return query.call(this, this.QueryGenerator.updateQuery(this.__definition.tableName, this.values, identifier))
return this.QueryInterface.update(this, this.__definition.tableName, this.values, identifier)
}
}
......
......@@ -42,20 +42,32 @@ module.exports = (function() {
}
QueryInterface.prototype.insert = function() {
QueryInterface.prototype.insert = function(model, tableName, values) {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
query
.call(self, self.QueryGenerator.insertQuery(tableName, values), model)
.success(function(obj) {
obj.isNewRecord = false
emitter.emit('success', obj)
})
.error(function(err) { emitter.emit('failure', err) })
}).run()
}
QueryInterface.prototype.update = function() {
QueryInterface.prototype.update = function(model, tableName, values, identifier) {
var sql = this.QueryGenerator.updateQuery(tableName, values, identifier)
return query.call(this, sql, model)
}
QueryInterface.prototype.destroy = function() {
}
QueryInterface.prototype.select = function(factory, tableName, options) {
return query.call(this, this.QueryGenerator.selectQuery(tableName, options), factory)
QueryInterface.prototype.select = function(factory, tableName, options, queryOptions) {
queryOptions = queryOptions || {}
return query.call(this, this.QueryGenerator.selectQuery(tableName, options), factory, queryOptions)
}
QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector) {
......@@ -75,35 +87,6 @@ module.exports = (function() {
}).run()
}
QueryInterface.prototype.count = function(tableName, options) {
var self = this
, countOption = "count(*) as count"
options = options || {}
options.attributes = options.attributes || []
options.attributes.push(['count(*)', 'count'])
return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options)
query.call(self, sql, factory, { plain: true }).success(function(data) {
emitter.emit('success', data.count)
}).error(function(err) {
emitter.emit('failure', err)
})
}).run()
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).success(function(obj) {
emitter.emit('success', obj['count(*)'])
})
})
return emitter.run()
}
// private
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!