* Update multiple instances that match the where options. * * @param {object} values hash of values to update * @param {object} options update options * @param {object} options.where Options to
(values, options)
| 3071 | * |
| 3072 | */ |
| 3073 | static async update(values, options) { |
| 3074 | options = Utils.cloneDeep(options); |
| 3075 | |
| 3076 | this._injectScope(options); |
| 3077 | this._optionsMustContainWhere(options); |
| 3078 | |
| 3079 | options = this._paranoidClause(this, _.defaults(options, { |
| 3080 | validate: true, |
| 3081 | hooks: true, |
| 3082 | individualHooks: false, |
| 3083 | returning: false, |
| 3084 | force: false, |
| 3085 | sideEffects: true |
| 3086 | })); |
| 3087 | |
| 3088 | options.type = QueryTypes.BULKUPDATE; |
| 3089 | |
| 3090 | // Clone values so it doesn't get modified for caller scope and ignore undefined values |
| 3091 | values = _.omitBy(values, value => value === undefined); |
| 3092 | |
| 3093 | // Remove values that are not in the options.fields |
| 3094 | if (options.fields && options.fields instanceof Array) { |
| 3095 | for (const key of Object.keys(values)) { |
| 3096 | if (!options.fields.includes(key)) { |
| 3097 | delete values[key]; |
| 3098 | } |
| 3099 | } |
| 3100 | } else { |
| 3101 | const updatedAtAttr = this._timestampAttributes.updatedAt; |
| 3102 | options.fields = _.intersection(Object.keys(values), Object.keys(this.tableAttributes)); |
| 3103 | if (updatedAtAttr && !options.fields.includes(updatedAtAttr)) { |
| 3104 | options.fields.push(updatedAtAttr); |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | if (this._timestampAttributes.updatedAt && !options.silent) { |
| 3109 | values[this._timestampAttributes.updatedAt] = this._getDefaultTimestamp(this._timestampAttributes.updatedAt) || Utils.now(this.sequelize.options.dialect); |
| 3110 | } |
| 3111 | |
| 3112 | options.model = this; |
| 3113 | |
| 3114 | let valuesUse; |
| 3115 | // Validate |
| 3116 | if (options.validate) { |
| 3117 | const build = this.build(values); |
| 3118 | build.set(this._timestampAttributes.updatedAt, values[this._timestampAttributes.updatedAt], { raw: true }); |
| 3119 | |
| 3120 | if (options.sideEffects) { |
| 3121 | Object.assign(values, _.pick(build.get(), build.changed())); |
| 3122 | options.fields = _.union(options.fields, Object.keys(values)); |
| 3123 | } |
| 3124 | |
| 3125 | // We want to skip validations for all other fields |
| 3126 | options.skip = _.difference(Object.keys(this.rawAttributes), Object.keys(values)); |
| 3127 | const attributes = await build.validate(options); |
| 3128 | options.skip = undefined; |
| 3129 | if (attributes && attributes.dataValues) { |
| 3130 | values = _.pick(attributes.dataValues, Object.keys(values)); |