Decrementing

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

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.decrement('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.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
  3. }).then(/* ... */)

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

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