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

Commit b1e94268 by Mick Hansen

Merge pull request #1616 from seegno/add-increment-where

Add filtering support to increment/decrement
2 parents 9a58695f f62ad887
Showing with 18 additions and 2 deletions
......@@ -548,6 +548,7 @@ module.exports = (function() {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
, updatedAtAttr = this.Model._timestampAttributes.updatedAt
, values = {}
, where
if (countOrOptions === undefined) {
countOrOptions = { by: 1, transaction: null }
......@@ -557,9 +558,12 @@ module.exports = (function() {
countOrOptions = Utils._.extend({
by: 1,
attributes: {}
attributes: {},
where: {}
}, countOrOptions)
where = _.extend(countOrOptions.where, identifier);
if (Utils._.isString(fields)) {
values[fields] = countOrOptions.by
} else if (Utils._.isArray(fields)) {
......@@ -574,7 +578,7 @@ module.exports = (function() {
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) {
......
......@@ -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) {
var self = this
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!