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

Commit 850e9093 by Mick Hansen

refactor(model): changed signature of update from (values, where, options) to (values, options)

1 parent 24f89d06
......@@ -148,7 +148,9 @@ module.exports = (function() {
if (changedAssociations.length > 0) {
changedAssociations.forEach(function(assoc) {
promises.push(self.association.through.model.update(assoc.attributes, assoc.where, options));
promises.push(self.association.through.model.update(assoc.attributes, Utils._.extend(options, {
where: assoc.where
})));
});
}
......@@ -176,7 +178,9 @@ module.exports = (function() {
attributes = Utils._.defaults({}, newAssociation[targetAssociation.through.model.name], additionalAttributes);
if (Object.keys(attributes).length) {
return targetAssociation.through.model.update(attributes, where, options);
return targetAssociation.through.model.update(attributes, Utils._.extend(options, {
where: where
}));
} else {
return Utils.Promise.resolve();
}
......
......@@ -73,8 +73,10 @@ module.exports = (function() {
updateWhere[primaryKey] = obsoleteIds;
promises.push(this.association.target.update(
update,
updateWhere,
Utils._.extend(options, { allowNull: [self.association.identifier] })
Utils._.extend(options, {
allowNull: [self.association.identifier],
where: updateWhere
})
));
}
......@@ -105,8 +107,10 @@ module.exports = (function() {
promises.push(this.association.target.update(
update,
updateWhere,
Utils._.extend(options, { allowNull: [self.association.identifier] })
Utils._.extend(options, {
allowNull: [self.association.identifier],
where: updateWhere
})
));
}
......
......@@ -1371,8 +1371,8 @@ module.exports = (function() {
* of affected rows, while the second element is the actual affected rows (only supported in postgres with `options.returning` true.)
*
* @param {Object} attrValueHash A hash of fields to change and their new values
* @param {Object where Options to describe the scope of the search. Note that these options are not wrapped in a { where: ... } is in find / findAll calls etc. This is probably due to change in 2.0
* @param {Object} [options]
* @param {Object} options
* @param {Object options.where Options to describe the scope of the search.
* @param {Boolean} [options.validate=true] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation
* @param {Boolean} [options.hooks=true] Run before / after bulk update hooks?
* @param {Boolean} [options.individualHooks=false] Run before / after update hooks?
......@@ -1382,9 +1382,13 @@ module.exports = (function() {
*
* @return {Promise<Array<affectedCount,affectedRows>>}
*/
Model.prototype.update = function(attrValueHash, where, options) {
Model.prototype.update = function(attrValueHash, options) {
var self = this;
if (!options || !options.where) {
throw new Error('Missing where attribute in the options parameter passed to update.');
}
options = Utils._.extend({
validate: true,
hooks: true,
......@@ -1420,12 +1424,9 @@ module.exports = (function() {
}).then(function() {
// Run before hook
if (options.hooks) {
options.where = where;
options.attributes = attrValueHash;
return self.runHooks('beforeBulkUpdate', options).then(function() {
where = options.where;
attrValueHash = options.attributes;
delete options.where;
delete options.attributes;
});
}
......@@ -1434,7 +1435,7 @@ module.exports = (function() {
// Get daos and run beforeUpdate hook on each record individually
if (options.individualHooks) {
return self.findAll({where: where}, {transaction: options.transaction}).then(function(_daos) {
return self.findAll({where: options.where}, {transaction: options.transaction}).then(function(_daos) {
daos = _daos;
if (!daos.length) {
return [];
......@@ -1502,7 +1503,7 @@ module.exports = (function() {
}
// Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHashUse, where, options, self.tableAttributes).then(function(affectedRows) {
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHashUse, options.where, options, self.tableAttributes).then(function(affectedRows) {
if (options.returning) {
daos = affectedRows;
return [affectedRows.length, affectedRows];
......@@ -1521,10 +1522,8 @@ module.exports = (function() {
}).tap(function() {
// Run after hook
if (options.hooks) {
options.where = where;
options.attributes = attrValueHash;
return self.runHooks('afterBulkUpdate', options).then(function() {
delete options.where;
delete options.attributes;
});
}
......
......@@ -764,7 +764,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
User.sync({ force: true }).done(function() {
User.create({ username: 'foo' }).done(function() {
sequelize.transaction().then(function(t) {
User.update({ username: 'bar' }, {}, { transaction: t }).done(function(err) {
User.update({ username: 'bar' }, {where: {username: 'foo'}, transaction: t }).done(function(err) {
User.all().done(function(err, users1) {
User.all({ transaction: t }).done(function(err, users2) {
expect(users1[0].username).to.equal('foo')
......@@ -826,7 +826,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'})
self.User.update({username: 'Bill'}, {where: {secretValue: '42'}})
.success(function() {
self.User.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3)
......@@ -851,7 +851,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.create({
username: 'John'
}).success(function(user) {
self.User.update({username: self.sequelize.cast('1', 'char')}, {username: 'John'}).success(function() {
self.User.update({username: self.sequelize.cast('1', 'char')}, {where: {username: 'John'}}).success(function() {
self.User.all().success(function(users) {
expect(users[0].username).to.equal('1')
done()
......@@ -866,7 +866,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.create({
username: 'John'
}).success(function(user) {
self.User.update({username: self.sequelize.fn('upper', self.sequelize.col('username'))}, {username: 'John'}).success(function () {
self.User.update({username: self.sequelize.fn('upper', self.sequelize.col('username'))}, {where: {username: 'John'}}).success(function () {
self.User.all().success(function(users) {
expect(users[0].username).to.equal('JOHN')
done()
......@@ -893,7 +893,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
// Pass the time so we can actually see a change
this.clock.tick(1000);
return this.User.update({username: 'Bill'}, {secretValue: '42'});
return this.User.update({username: 'Bill'}, {where: {secretValue: '42'}});
}).then(function () {
return this.User.findAll({order: 'id'});
}).then(function (users) {
......@@ -916,13 +916,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, done = _.after(2, _done)
this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).spread(function(affectedRows) {
self.User.update({username: 'Bill'}, {where: {secretValue: '42'}}).spread(function(affectedRows) {
expect(affectedRows).to.equal(2)
done()
})
self.User.update({username: 'Bill'}, {secretValue: '44'}).spread(function(affectedRows) {
self.User.update({username: 'Bill'}, {where: {secretValue: '44'}}).spread(function(affectedRows) {
expect(affectedRows).to.equal(0)
done()
......@@ -939,14 +939,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, done = _.after(2, _done)
this.User.bulkCreate(data).success(function() {
self.User.update({ username: 'Bill' }, { secretValue: '42' }, { returning: true }).spread(function(count, rows) {
self.User.update({ username: 'Bill' }, { where: {secretValue: '42' }, returning: true }).spread(function(count, rows) {
expect(count).to.equal(2)
expect(rows).to.have.length(2)
done()
})
self.User.update({ username: 'Bill'}, { secretValue: '44' }, { returning: true }).spread(function(count, rows) {
self.User.update({ username: 'Bill'}, { where: {secretValue: '44' }, returning: true }).spread(function(count, rows) {
expect(count).to.equal(0)
expect(rows).to.have.length(0)
......@@ -964,7 +964,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
{ username: 'Peter', secretValue: '42' }]
this.User.bulkCreate(data).success(function () {
self.User.update({secretValue: '43'}, {username: 'Peter'}, {limit: 1}).spread(function(affectedRows) {
self.User.update({secretValue: '43'}, {where: {username: 'Peter'}, limit: 1}).spread(function(affectedRows) {
expect(affectedRows).to.equal(1)
done()
})
......
......@@ -163,7 +163,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
this.User.create({mood: 'sad'}).success(function() {
self.User.update({mood: 'ecstatic'}, {username: 'Toni'}, {validate: true}).success(function() {
self.User.update({mood: 'ecstatic'}, {where: {username: 'Toni'}, validate: true}).success(function() {
self.User.find({where: {username: 'Toni'}}).success(function(user) {
expect(beforeBulkUpdate).to.be.true
expect(afterBulkUpdate).to.be.true
......@@ -3275,7 +3275,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).success(function() {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).success(function() {
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.true
done()
......@@ -3296,7 +3296,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3314,7 +3314,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3349,7 +3349,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).success(function() {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).success(function() {
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.true
done()
......@@ -3374,7 +3374,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3396,7 +3396,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3428,7 +3428,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).success(function() {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).success(function() {
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.true
done()
......@@ -3449,7 +3449,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3467,7 +3467,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3502,7 +3502,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).success(function() {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).success(function() {
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.true
done()
......@@ -3527,7 +3527,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3549,7 +3549,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).success(function() {
self.User.update({mood: 'happy'}, {mood: 'sad'}).error(function(err) {
self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).error(function(err) {
expect(err).to.be.instanceOf(Error)
done()
})
......@@ -3647,7 +3647,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
this.User.bulkCreate([{aNumber: 1}, {aNumber: 1}, {aNumber: 1}], { fields: ['aNumber'] }).success(function() {
self.User.update({aNumber: 10}, {aNumber: 1}, {individualHooks: true}).error(function(err) {
self.User.update({aNumber: 10}, {where: {aNumber: 1}, individualHooks: true}).error(function(err) {
expect(err).to.be.instanceOf(Error)
expect(beforeBulk).to.be.true
expect(afterBulk).to.be.false
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!