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

Commit 19a3010c by Mick Hansen

feat(instance): make it possible to pass createdAt and updatedAt to Model.create…

… when using silent: true
1 parent ffaafdc2
...@@ -489,7 +489,7 @@ module.exports = (function() { ...@@ -489,7 +489,7 @@ module.exports = (function() {
options.fields.push(updatedAtAttr); options.fields.push(updatedAtAttr);
} }
if (options.silent === true) { if (options.silent === true && !(this.isNewRecord && this.get(updatedAtAttr, {raw: true}))) {
// UpdateAtAttr might have been added as a result of Object.keys(Model.attributes). In that case we have to remove it again // UpdateAtAttr might have been added as a result of Object.keys(Model.attributes). In that case we have to remove it again
Utils._.remove(options.fields, function(val) { Utils._.remove(options.fields, function(val) {
return val === updatedAtAttr; return val === updatedAtAttr;
...@@ -537,7 +537,7 @@ module.exports = (function() { ...@@ -537,7 +537,7 @@ module.exports = (function() {
} }
} }
if (updatedAtAttr) { if (updatedAtAttr && !options.silent) {
values[updatedAtAttr] = self.Model.__getTimestamp(updatedAtAttr); values[updatedAtAttr] = self.Model.__getTimestamp(updatedAtAttr);
} }
......
...@@ -1051,7 +1051,9 @@ module.exports = (function() { ...@@ -1051,7 +1051,9 @@ module.exports = (function() {
return this.build(values, { return this.build(values, {
isNewRecord: true, isNewRecord: true,
attributes: options.fields, attributes: options.fields,
include: options.include include: options.include,
raw: options.raw,
silent: options.silent
}).save(options); }).save(options);
}; };
......
...@@ -294,6 +294,38 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -294,6 +294,38 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('should be able to set createdAt and updatedAt if using silent: true', function () {
var User = this.sequelize.define('user', {
name: DataTypes.STRING
}, {
timestamps: true
});
var createdAt = new Date(2012, 10, 10, 10, 10, 10);
var updatedAt = new Date(2011, 11, 11, 11, 11, 11);
return User.sync({force: true}).then(function () {
return User.create({
createdAt: createdAt,
updatedAt: updatedAt
}, {
silent: true
}).then(function (user) {
expect(createdAt.getTime()).to.equal(user.get('createdAt').getTime());
expect(updatedAt.getTime()).to.equal(user.get('updatedAt').getTime());
return User.findOne({
updatedAt: {
ne: null
}
}).then(function (user) {
expect(createdAt.getTime()).to.equal(user.get('createdAt').getTime());
expect(updatedAt.getTime()).to.equal(user.get('updatedAt').getTime());
});
});
});
});
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function(done) { it('supports transactions', function(done) {
var self = this; var self = this;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!