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

Commit 38a1026b by Yu Qi Committed by Jan Aagaard Meier

add options.silent support for Instance increment and decrement. (#6795)

1 parent bcc25be7
Showing with 37 additions and 1 deletions
...@@ -3650,6 +3650,7 @@ class Model { ...@@ -3650,6 +3650,7 @@ class Model {
* @param {String|Array|Object} fields If a string is provided, that column is incremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is incremented by the value given. * @param {String|Array|Object} fields If a string is provided, that column is incremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is incremented by the value given.
* @param {Object} [options] * @param {Object} [options]
* @param {Integer} [options.by=1] The number to increment by * @param {Integer} [options.by=1] The number to increment by
* @param {Boolean} [options.silent=false] If true, the updatedAt timestamp will not be updated.
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql. * @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction] * @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only) * @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
...@@ -3679,7 +3680,7 @@ class Model { ...@@ -3679,7 +3680,7 @@ class Model {
values = fields; values = fields;
} }
if (updatedAtAttr && !values[updatedAtAttr]) { if (!options.silent && updatedAtAttr && !values[updatedAtAttr]) {
options.attributes[updatedAtAttr] = this.constructor._getDefaultTimestamp(updatedAtAttr) || Utils.now(this.sequelize.options.dialect); options.attributes[updatedAtAttr] = this.constructor._getDefaultTimestamp(updatedAtAttr) || Utils.now(this.sequelize.options.dialect);
} }
...@@ -3712,6 +3713,7 @@ class Model { ...@@ -3712,6 +3713,7 @@ class Model {
* @param {String|Array|Object} fields If a string is provided, that column is decremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is decremented by the value given * @param {String|Array|Object} fields If a string is provided, that column is decremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is decremented by the value given
* @param {Object} [options] * @param {Object} [options]
* @param {Integer} [options.by=1] The number to decrement by * @param {Integer} [options.by=1] The number to decrement by
* @param {Boolean} [options.silent=false] If true, the updatedAt timestamp will not be updated.
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql. * @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction] * @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only) * @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
......
...@@ -264,6 +264,23 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -264,6 +264,23 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate); return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate);
}); });
}); });
it('with timestamps set to true and options.silent set to true', function() {
var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true })
, oldDate;
return User.sync({ force: true }).bind(this).then(function() {
return User.create({aNumber: 1});
}).then(function(user) {
oldDate = user.updatedAt;
this.clock.tick(1000);
return user.increment('aNumber', {by: 1, silent: true});
}).then(function() {
return expect(User.findById(1)).to.eventually.have.property('updatedAt').equalTime(oldDate);
});
});
}); });
describe('decrement', function() { describe('decrement', function() {
...@@ -389,6 +406,23 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -389,6 +406,23 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate); return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate);
}); });
}); });
it('with timestamps set to true and options.silent set to true', function() {
var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true })
, oldDate;
return User.sync({ force: true }).bind(this).then(function() {
return User.create({aNumber: 1});
}).then(function(user) {
oldDate = user.updatedAt;
this.clock.tick(1000);
return user.decrement('aNumber', {by: 1, silent: true});
}).then(function() {
return expect(User.findById(1)).to.eventually.have.property('updatedAt').equalTime(oldDate);
});
});
}); });
describe('reload', function() { describe('reload', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!