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

Method increment

src/model.js:3452–3533  ·  view source on GitHub ↗

* 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

3450 * @returns {Promise<Model[],?number>} returns an array of affected rows and affected count with `options.returning` true, whenever supported by dialect
3451 */
3452 static async increment(fields, options) {
3453 options = options || {};
3454 if (typeof fields === 'string') fields = [fields];
3455 if (Array.isArray(fields)) {
3456 fields = fields.map(f => {
3457 if (this.rawAttributes[f] && this.rawAttributes[f].field && this.rawAttributes[f].field !== f) {
3458 return this.rawAttributes[f].field;
3459 }
3460 return f;
3461 });
3462 } else if (fields && typeof fields === 'object') {
3463 fields = Object.keys(fields).reduce((rawFields, f) => {
3464 if (this.rawAttributes[f] && this.rawAttributes[f].field && this.rawAttributes[f].field !== f) {
3465 rawFields[this.rawAttributes[f].field] = fields[f];
3466 } else {
3467 rawFields[f] = fields[f];
3468 }
3469 return rawFields;
3470 }, {});
3471 }
3472
3473 this._injectScope(options);
3474 this._optionsMustContainWhere(options);
3475
3476 options = Utils.defaults({}, options, {
3477 by: 1,
3478 where: {},
3479 increment: true
3480 });
3481 const isSubtraction = !options.increment;
3482
3483 Utils.mapOptionFieldNames(options, this);
3484
3485 const where = { ...options.where };
3486
3487 // A plain object whose keys are the fields to be incremented and whose values are
3488 // the amounts to be incremented by.
3489 let incrementAmountsByField = {};
3490 if (Array.isArray(fields)) {
3491 incrementAmountsByField = {};
3492 for (const field of fields) {
3493 incrementAmountsByField[field] = options.by;
3494 }
3495 } else {
3496 // If the `fields` argument is not an array, then we assume it already has the
3497 // form necessary to be placed directly in the `incrementAmountsByField` variable.
3498 incrementAmountsByField = fields;
3499 }
3500
3501 // If optimistic locking is enabled, we can take advantage that this is an
3502 // increment/decrement operation and send it here as well. We put `-1` for
3503 // decrementing because it will be subtracted, getting `-(-1)` which is `+1`
3504 if (this._versionAttribute) {
3505 incrementAmountsByField[this._versionAttribute] = isSubtraction ? -1 : 1;
3506 }
3507
3508 const extraAttributesToBeUpdated = {};
3509

Callers 9

decrementMethod · 0.95
schema.test.jsFile · 0.45
field.test.jsFile · 0.45
increment.test.jsFile · 0.45
model.tsFile · 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