Addition and Subtraction Assignment Operator With Sequelize

瘦欲@ 提交于 2019-11-28 14:34:32

Here it is :

db.table.update({ field: Sequelize.literal('data + 1') }, { where: { id: 1 }}))

OR

User.findById(1).then(user => {
  // -----> First Way
  return user.increment('my-integer-field', {by: 2});
  // -----> Second Way
  return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
  // -----> Third Way
  return user.increment({
     'my-integer-field':    2,
     'my-very-other-field': 3
  })
});

You can also do decrement by just replacing increment with decrement.

For more detail : DO READ

Sequelize increment & decrement

myIncrementFunc(id, incrementor) {
  User.findById(id).then(user => {
    return user.increment('tableColumnName', {by: incrementor})
  }).then(user => {})
}

Code taken form sequelize Instances tutorial for Incrementing & Decrementing: http://docs.sequelizejs.com/manual/tutorial/instances.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!