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

Commit 2ad7673d by Mick Hansen

Merge pull request #2501 from tiblu/model_update_doc_fixes

Model.update - changed parameter "attrValueHash" to "values" for consist...
2 parents 2bbf6397 e8f2b9a1
Showing with 16 additions and 17 deletions
...@@ -1390,7 +1390,7 @@ module.exports = (function() { ...@@ -1390,7 +1390,7 @@ module.exports = (function() {
* Update multiple instances that match the where options. The promise returns an array with one or two elements. The first element is always the number * Update multiple instances that match the where options. The promise returns an array with one or two elements. The first element is always the number
* of affected rows, while the second element is the actual affected rows (only supported in postgres with `options.returning` true.) * 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} values
* @param {Object} options * @param {Object} options
* @param {Object options.where Options to describe the scope of the search. * @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.validate=true] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation
...@@ -1398,11 +1398,10 @@ module.exports = (function() { ...@@ -1398,11 +1398,10 @@ module.exports = (function() {
* @param {Boolean} [options.individualHooks=false] Run before / after update hooks? * @param {Boolean} [options.individualHooks=false] Run before / after update hooks?
* @param {Boolean} [options.returning=false] Return the affected rows (only for postgres) * @param {Boolean} [options.returning=false] Return the affected rows (only for postgres)
* @param {Number} [options.limit] How many rows to update (only for mysql and mariadb) * @param {Number} [options.limit] How many rows to update (only for mysql and mariadb)
* @deprecated The syntax is due for change, in order to make `where` more consistent with the rest of the API
* *
* @return {Promise<Array<affectedCount,affectedRows>>} * @return {Promise<Array<affectedCount,affectedRows>>}
*/ */
Model.prototype.update = function(attrValueHash, options) { Model.prototype.update = function(values, options) {
var self = this; var self = this;
if (!options || !options.where) { if (!options || !options.where) {
...@@ -1420,38 +1419,38 @@ module.exports = (function() { ...@@ -1420,38 +1419,38 @@ module.exports = (function() {
options.type = QueryTypes.BULKUPDATE; options.type = QueryTypes.BULKUPDATE;
if (self._timestampAttributes.updatedAt) { if (self._timestampAttributes.updatedAt) {
attrValueHash[self._timestampAttributes.updatedAt] = Utils.now(self.modelManager.sequelize.options.dialect); values[self._timestampAttributes.updatedAt] = Utils.now(self.modelManager.sequelize.options.dialect);
} }
var daos var daos
, attrValueHashUse; , valuesUse;
return Promise.try(function() { return Promise.try(function() {
// Validate // Validate
if (options.validate) { if (options.validate) {
var build = self.build(attrValueHash); var build = self.build(values);
build.set(self._timestampAttributes.updatedAt, attrValueHash[self._timestampAttributes.updatedAt], { raw: true }); build.set(self._timestampAttributes.updatedAt, values[self._timestampAttributes.updatedAt], { raw: true });
// We want to skip validations for all other fields // We want to skip validations for all other fields
options.skip = Utils._.difference(Object.keys(self.attributes), Object.keys(attrValueHash)); options.skip = Utils._.difference(Object.keys(self.attributes), Object.keys(values));
return build.hookValidate(options).then(function(attributes) { return build.hookValidate(options).then(function(attributes) {
delete options.skip; delete options.skip;
if (attributes && attributes.dataValues) { if (attributes && attributes.dataValues) {
attrValueHash = Utils._.pick(attributes.dataValues, Object.keys(attrValueHash)); values = Utils._.pick(attributes.dataValues, Object.keys(values));
} }
}); });
} }
}).then(function() { }).then(function() {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
options.attributes = attrValueHash; options.attributes = values;
return self.runHooks('beforeBulkUpdate', options).then(function() { return self.runHooks('beforeBulkUpdate', options).then(function() {
attrValueHash = options.attributes; values = options.attributes;
delete options.attributes; delete options.attributes;
}); });
} }
}).then(function() { }).then(function() {
attrValueHashUse = attrValueHash; valuesUse = values;
// Get daos and run beforeUpdate hook on each record individually // Get daos and run beforeUpdate hook on each record individually
if (options.individualHooks) { if (options.individualHooks) {
...@@ -1468,7 +1467,7 @@ module.exports = (function() { ...@@ -1468,7 +1467,7 @@ module.exports = (function() {
return Promise.map(daos, function(dao) { return Promise.map(daos, function(dao) {
// Record updates in dao's dataValues // Record updates in dao's dataValues
Utils._.extend(dao.dataValues, attrValueHash); Utils._.extend(dao.dataValues, values);
// Run beforeUpdate hook // Run beforeUpdate hook
return self.runHooks('beforeUpdate', dao, options).then(function() { return self.runHooks('beforeUpdate', dao, options).then(function() {
...@@ -1495,8 +1494,8 @@ module.exports = (function() { ...@@ -1495,8 +1494,8 @@ module.exports = (function() {
if (!different) { if (!different) {
// Hooks do not change values or change them uniformly // Hooks do not change values or change them uniformly
if (Object.keys(changedValues).length) { if (Object.keys(changedValues).length) {
// Hooks change values - record changes in attrValueHashUse so they are executed // Hooks change values - record changes in valuesUse so they are executed
attrValueHashUse = changedValues; valuesUse = changedValues;
} }
return; return;
} else { } else {
...@@ -1523,7 +1522,7 @@ module.exports = (function() { ...@@ -1523,7 +1522,7 @@ module.exports = (function() {
} }
// Run query to update all rows // Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHashUse, options.where, options, self.tableAttributes).then(function(affectedRows) { return self.QueryInterface.bulkUpdate(self.getTableName(), valuesUse, options.where, options, self.tableAttributes).then(function(affectedRows) {
if (options.returning) { if (options.returning) {
daos = affectedRows; daos = affectedRows;
return [affectedRows.length, affectedRows]; return [affectedRows.length, affectedRows];
...@@ -1542,7 +1541,7 @@ module.exports = (function() { ...@@ -1542,7 +1541,7 @@ module.exports = (function() {
}).tap(function() { }).tap(function() {
// Run after hook // Run after hook
if (options.hooks) { if (options.hooks) {
options.attributes = attrValueHash; options.attributes = values;
return self.runHooks('afterBulkUpdate', options).then(function() { return self.runHooks('afterBulkUpdate', options).then(function() {
delete options.attributes; delete options.attributes;
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!