MCPcopy
hub / github.com/sequelize/sequelize / update

Method update

src/model.js:3191–3372  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

3189 *
3190 */
3191 static async update(values, options) {
3192 options = Utils.cloneDeep(options);
3193
3194 // Add CLS transaction
3195 if (options.transaction === undefined && this.sequelize.constructor._cls) {
3196 const t = this.sequelize.constructor._cls.get('transaction');
3197 if (t) {
3198 options.transaction = t;
3199 }
3200 }
3201
3202 this._injectScope(options);
3203 this._optionsMustContainWhere(options);
3204
3205 options = this._paranoidClause(this, _.defaults(options, {
3206 validate: true,
3207 hooks: true,
3208 individualHooks: false,
3209 returning: false,
3210 force: false,
3211 sideEffects: true
3212 }));
3213
3214 options.type = QueryTypes.BULKUPDATE;
3215
3216 // Clone values so it doesn't get modified for caller scope and ignore undefined values
3217 values = _.omitBy(values, value => value === undefined);
3218
3219 // Remove values that are not in the options.fields
3220 if (options.fields && options.fields instanceof Array) {
3221 for (const key of Object.keys(values)) {
3222 if (!options.fields.includes(key)) {
3223 delete values[key];
3224 }
3225 }
3226 } else {
3227 const updatedAtAttr = this._timestampAttributes.updatedAt;
3228 options.fields = _.intersection(Object.keys(values), Object.keys(this.tableAttributes));
3229 if (updatedAtAttr && !options.fields.includes(updatedAtAttr)) {
3230 options.fields.push(updatedAtAttr);
3231 }
3232 }
3233
3234 if (this._timestampAttributes.updatedAt && !options.silent) {
3235 values[this._timestampAttributes.updatedAt] = this._getDefaultTimestamp(this._timestampAttributes.updatedAt) || Utils.now(this.sequelize.options.dialect);
3236 }
3237
3238 options.model = this;
3239
3240 let valuesUse;
3241 // Validate
3242 if (options.validate) {
3243 const build = this.build(values);
3244 build.set(this._timestampAttributes.updatedAt, values[this._timestampAttributes.updatedAt], { raw: true });
3245
3246 if (options.sideEffects) {
3247 Object.assign(values, _.pick(build.get(), build.changed()));
3248 options.fields = _.union(options.fields, Object.keys(values));

Callers 15

executeFunction · 0.45
trigger.test.jsFile · 0.45
data-types.test.jsFile · 0.45
model.test.jsFile · 0.45
firstTransactionFunction · 0.45
secondTransactionFunction · 0.45
updateFunction · 0.45
verifyDeadlockFunction · 0.45
sequelize.test.jsFile · 0.45

Calls 15

_injectScopeMethod · 0.95
_paranoidClauseMethod · 0.95
_getDefaultTimestampMethod · 0.95
buildMethod · 0.95
changedMethod · 0.95
findAllMethod · 0.95
saveMethod · 0.95
getTableNameMethod · 0.95
setMethod · 0.95
allMethod · 0.80
setDataValueMethod · 0.80

Tested by 7

executeFunction · 0.36
firstTransactionFunction · 0.36
secondTransactionFunction · 0.36
updateFunction · 0.36
verifyDeadlockFunction · 0.36
testFunction · 0.36