Incrementing

In order to increment values of an instance without running into concurrency issues, you may use increment.

First of all you can define a field and the value you want to add to it.

  1. User.findByPk(1).then(user => {
  2. return user.increment('my-integer-field', {by: 2})
  3. }).then(user => {
  4. // Postgres will return the updated user by default (unless disabled by setting { returning: false })
  5. // In other dialects, you'll want to call user.reload() to get the updated instance...
  6. })

Second, you can define multiple fields and the value you want to add to them.

  1. User.findByPk(1).then(user => {
  2. return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
  3. }).then(/* ... */)

Third, you can define an object containing fields and its increment values.

  1. User.findByPk(1).then(user => {
  2. return user.increment({
  3. 'my-integer-field': 2,
  4. 'my-very-other-field': 3
  5. })
  6. }).then(/* ... */)