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

Commit 05b76ff7 by Sascha Depold

Merge branch 'master' of https://github.com/LaurentZuijdwijk/sequelize into LaurentZuijdwijk-master

2 parents 47611d44 d0d86f6b
var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
var Person = sequelize.define('Person',
{ name: Sequelize.STRING,
age : Sequelize.INTEGER
})
, chainer = new Sequelize.Utils.QueryChainer
sequelize.sync({force: true}).on('success', function() {
var count = 10,
queries = []
for(var i = 0; i < count; i++)
chainer.add(Person.create({name: 'someone' + (i % 3), age : i+5}))
console.log("Begin to save " + count + " items!")
chainer.run().on('success', function() {
console.log("finished")
Person.max('age').on('success', function(max) {
console.log("Oldest person: " + max)
});
Person.min('age').on('success', function(min) {
console.log("Youngest person: " + min)
});
})
})
\ No newline at end of file
...@@ -107,6 +107,26 @@ ModelDefinition.prototype.count = function(options) { ...@@ -107,6 +107,26 @@ ModelDefinition.prototype.count = function(options) {
return emitter.run() return emitter.run()
} }
ModelDefinition.prototype.max = function(field, options) {
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
self.query(QueryGenerator.maxQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['max'])
})
})
return emitter.run()
}
ModelDefinition.prototype.min = function(field, options) {
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
self.query(QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['min'])
})
})
return emitter.run()
}
ModelDefinition.prototype.findAll = function(options) { ModelDefinition.prototype.findAll = function(options) {
return this.query(QueryGenerator.selectQuery(this.tableName, options)) return this.query(QueryGenerator.selectQuery(this.tableName, options))
} }
......
...@@ -78,7 +78,12 @@ var QueryGenerator = module.exports = { ...@@ -78,7 +78,12 @@ var QueryGenerator = module.exports = {
countQuery: function(tableName, options) { countQuery: function(tableName, options) {
return QueryGenerator.selectQuery(tableName, options).replace("*", "count(*)") return QueryGenerator.selectQuery(tableName, options).replace("*", "count(*)")
}, },
maxQuery: function(tableName, field,options) {
return QueryGenerator.selectQuery(tableName ,options).replace("*", "max("+field+") as max")
},
minQuery: function(tableName, field,options) {
return QueryGenerator.selectQuery(tableName ,options).replace("*", "min("+field+") as min")
},
/* /*
Returns an insert into command. Parameters: table name + hash of attribute-value-pairs. Returns an insert into command. Parameters: table name + hash of attribute-value-pairs.
*/ */
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!