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

Commit 6817aad8 by Jan Aagaard Meier

Validation for upsert. Closes #2676

1 parent f5e3448a
# Next
- [BUG] Fixed issue with paranoid deletes and `deletedAt` with a custom field.
- [BUG] No longer crahes on `where: []`
- [FEATURE] Validations are now enabled by default for upsert.
# 2.0.0-rc7
- [FEATURE] Throw an error if no where clause is given to `Model.destroy()`.
......
......@@ -1197,6 +1197,7 @@ module.exports = (function() {
*
* @param {Object} values
* @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
*
* @alias insertOrUpdate
......@@ -1211,25 +1212,30 @@ module.exports = (function() {
var createdAtAttr = this._timestampAttributes.createdAt
, updatedAtAttr = this._timestampAttributes.updatedAt
, hadPrimary = this.primaryKeyField in values;
, hadPrimary = this.primaryKeyField in values
, instance;
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);
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = this.__getTimestamp(createdAtAttr);
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = this.__getTimestamp(updatedAtAttr);
}
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
// Build adds a null value for the primary key, if none was given by the user.
// We need to remove that because of some Postgres technicalities.
if (!hadPrimary) {
delete values[this.primaryKeyField];
}
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = this.__getTimestamp(createdAtAttr);
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = this.__getTimestamp(updatedAtAttr);
}
// Build adds a null value for the primary key, if none was given by the user.
// We need to remove that because of some Postgres technicalities.
if (!hadPrimary) {
delete values[this.primaryKeyField];
}
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;
......
......@@ -40,7 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
if (current.dialect.supports.upserts) {
describe('upsert', function() {
describe.only('upsert', function() {
it('works with upsert on id', function() {
return this.User.upsert({ id: 42, username: 'john' }).bind(this).then(function(created) {
if (dialect === 'sqlite') {
......@@ -90,7 +90,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
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!