MCPcopy
hub / github.com/sequelize/sequelize / increment

Method increment

lib/model.js:3326–3407  ·  lib/model.js::Model.increment

* Increment the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The increment is done using a * ``` SET column = column + X WHERE foo = 'bar' ``` query. To get the correct value after an increment into the Instan

(fields, options)

Source from the content-addressed store, hash-verified

3324 * @returns {Promise<Model[],?number>} returns an array of affected rows and affected count with `options.returning` true, whenever supported by dialect
3325 */
3326 static async increment(fields, options) {
3327 options = options || {};
3328 if (typeof fields === class="st">'string') fields = [fields];
3329 if (Array.isArray(fields)) {
3330 fields = fields.map(f => {
3331 if (this.rawAttributes[f] && this.rawAttributes[f].field && this.rawAttributes[f].field !== f) {
3332 return this.rawAttributes[f].field;
3333 }
3334 return f;
3335 });
3336 } else if (fields && typeof fields === class="st">'object') {
3337 fields = Object.keys(fields).reduce((rawFields, f) => {
3338 if (this.rawAttributes[f] && this.rawAttributes[f].field && this.rawAttributes[f].field !== f) {
3339 rawFields[this.rawAttributes[f].field] = fields[f];
3340 } else {
3341 rawFields[f] = fields[f];
3342 }
3343 return rawFields;
3344 }, {});
3345 }
3346
3347 this._injectScope(options);
3348 this._optionsMustContainWhere(options);
3349
3350 options = Utils.defaults({}, options, {
3351 by: 1,
3352 where: {},
3353 increment: true
3354 });
3355 const isSubtraction = !options.increment;
3356
3357 Utils.mapOptionFieldNames(options, this);
3358
3359 const where = { ...options.where };
3360
3361 class="cm">// A plain object whose keys are the fields to be incremented and whose values are
3362 class="cm">// the amounts to be incremented by.
3363 let incrementAmountsByField = {};
3364 if (Array.isArray(fields)) {
3365 incrementAmountsByField = {};
3366 for (const field of fields) {
3367 incrementAmountsByField[field] = options.by;
3368 }
3369 } else {
3370 class="cm">// If the `fields` argument is not an array, then we assume it already has the
3371 class="cm">// form necessary to be placed directly in the `incrementAmountsByField` variable.
3372 incrementAmountsByField = fields;
3373 }
3374
3375 class="cm">// If optimistic locking is enabled, we can take advantage that this is an
3376 class="cm">// increment/decrement operation and send it here as well. We put `-1` for
3377 class="cm">// decrementing because it will be subtracted, getting `-(-1)` which is `+1`
3378 if (this._versionAttribute) {
3379 incrementAmountsByField[this._versionAttribute] = isSubtraction ? -1 : 1;
3380 }
3381
3382 const extraAttributesToBeUpdated = {};
3383

Callers 7

decrementMethod · 0.95
schema.test.jsFile · 0.45
field.test.jsFile · 0.45
increment.test.jsFile · 0.45
increment.test.jsFile · 0.45
increment.test.jsFile · 0.45

Calls 6

_injectScopeMethod · 0.95
_getDefaultTimestampMethod · 0.95
getTableNameMethod · 0.95
whereMethod · 0.95
decrementMethod · 0.45

Tested by

no test coverage detected