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

Commit f62ad887 by Tiago Ribeiro

Add filtering support to increment/decrement

1 parent c6bd7b2e
Showing with 18 additions and 2 deletions
...@@ -546,6 +546,7 @@ module.exports = (function() { ...@@ -546,6 +546,7 @@ module.exports = (function() {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id } var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
, updatedAtAttr = this.Model._timestampAttributes.updatedAt , updatedAtAttr = this.Model._timestampAttributes.updatedAt
, values = {} , values = {}
, where
if (countOrOptions === undefined) { if (countOrOptions === undefined) {
countOrOptions = { by: 1, transaction: null } countOrOptions = { by: 1, transaction: null }
...@@ -555,9 +556,12 @@ module.exports = (function() { ...@@ -555,9 +556,12 @@ module.exports = (function() {
countOrOptions = Utils._.extend({ countOrOptions = Utils._.extend({
by: 1, by: 1,
attributes: {} attributes: {},
where: {}
}, countOrOptions) }, countOrOptions)
where = _.extend(countOrOptions.where, identifier);
if (Utils._.isString(fields)) { if (Utils._.isString(fields)) {
values[fields] = countOrOptions.by values[fields] = countOrOptions.by
} else if (Utils._.isArray(fields)) { } else if (Utils._.isArray(fields)) {
...@@ -572,7 +576,7 @@ module.exports = (function() { ...@@ -572,7 +576,7 @@ module.exports = (function() {
countOrOptions.attributes[updatedAtAttr] = Utils.now(this.Model.daoFactoryManager.sequelize.options.dialect) countOrOptions.attributes[updatedAtAttr] = Utils.now(this.Model.daoFactoryManager.sequelize.options.dialect)
} }
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model.tableName, this.Model.options.schema), values, identifier, countOrOptions) return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model.tableName, this.Model.options.schema), values, where, countOrOptions)
} }
DAO.prototype.decrement = function (fields, countOrOptions) { DAO.prototype.decrement = function (fields, countOrOptions) {
......
...@@ -294,6 +294,18 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -294,6 +294,18 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
}) })
it('supports where conditions', function(done) {
var self = this
this.User.find(1).complete(function(err, user1) {
user1.increment(['aNumber'], { by: 2, where: { bNumber: 1 } }).complete(function() {
self.User.find(1).complete(function(err, user3) {
expect(user3.aNumber).to.be.equal(0)
done()
})
})
})
})
it('with array', function(done) { it('with array', function(done) {
var self = this var self = this
this.User.find(1).complete(function(err, user1) { this.User.find(1).complete(function(err, user1) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!