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

Commit 6817aad8 by Jan Aagaard Meier

Validation for upsert. Closes #2676

1 parent f5e3448a
# Next # Next
- [BUG] Fixed issue with paranoid deletes and `deletedAt` with a custom field. - [BUG] Fixed issue with paranoid deletes and `deletedAt` with a custom field.
- [BUG] No longer crahes on `where: []` - [BUG] No longer crahes on `where: []`
- [FEATURE] Validations are now enabled by default for upsert.
# 2.0.0-rc7 # 2.0.0-rc7
- [FEATURE] Throw an error if no where clause is given to `Model.destroy()`. - [FEATURE] Throw an error if no where clause is given to `Model.destroy()`.
......
...@@ -1197,6 +1197,7 @@ module.exports = (function() { ...@@ -1197,6 +1197,7 @@ module.exports = (function() {
* *
* @param {Object} values * @param {Object} values
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean}[options.validate=true] Run validations before the row is insertedF
* @param {Array} [options.fields=Object.keys(this.attributes)] The fields to insert / update. Defaults to all fields * @param {Array} [options.fields=Object.keys(this.attributes)] The fields to insert / update. Defaults to all fields
* *
* @alias insertOrUpdate * @alias insertOrUpdate
...@@ -1211,10 +1212,14 @@ module.exports = (function() { ...@@ -1211,10 +1212,14 @@ module.exports = (function() {
var createdAtAttr = this._timestampAttributes.createdAt var createdAtAttr = this._timestampAttributes.createdAt
, updatedAtAttr = this._timestampAttributes.updatedAt , updatedAtAttr = this._timestampAttributes.updatedAt
, hadPrimary = this.primaryKeyField in values; , hadPrimary = this.primaryKeyField in values
, instance;
values = Utils._.pick(values, options.fields); values = Utils._.pick(values, options.fields);
values = this.build(values).get(); // Get default values - this also adds a null value for the PK if none is given instance = this.build(values);
return instance.hookValidate(options).bind(this).then(function () {
values = instance.get(); // Get default values - this also adds a null value for the PK if none is given
if (createdAtAttr && !values[createdAtAttr]) { if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = this.__getTimestamp(createdAtAttr); values[createdAtAttr] = this.__getTimestamp(createdAtAttr);
...@@ -1230,6 +1235,7 @@ module.exports = (function() { ...@@ -1230,6 +1235,7 @@ module.exports = (function() {
} }
return this.QueryInterface.upsert(this.getTableName(options), values, this, options); return this.QueryInterface.upsert(this.getTableName(options), values, this, options);
});
}; };
Model.prototype.insertOrUpdate = Model.prototype.upsert; Model.prototype.insertOrUpdate = Model.prototype.upsert;
......
...@@ -40,7 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -40,7 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
if (current.dialect.supports.upserts) { if (current.dialect.supports.upserts) {
describe('upsert', function() { describe.only('upsert', function() {
it('works with upsert on id', function() { it('works with upsert on id', function() {
return this.User.upsert({ id: 42, username: 'john' }).bind(this).then(function(created) { return this.User.upsert({ id: 42, username: 'john' }).bind(this).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
...@@ -90,7 +90,19 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -90,7 +90,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(user.updatedAt).to.be.afterTime(user.createdAt); expect(user.updatedAt).to.be.afterTime(user.createdAt);
}); });
}); });
});
it('supports validations', function () {
var User = this.sequelize.define('user', {
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
} }
}
});
return expect(User.upsert({ email: 'notanemail' })).to.eventually.be.rejectedWith(this.sequelize.ValidationError);
});
});
}
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!