使用Sequelize

时间:2018-02-14 02:33:39

标签: node.js sequelize.js variable-assignment addition subtraction

我想通过在Sequelize上做一个简单的添加来做更新。

表:

id || data
 1 ||  10 

样品:

db.table.update({ data : 1 }, { where: { id: 1 }});

在此查询之后

id || data
 1 ||  11

我知道这是一个简单的问题,但我找不到解决方案。

我可以添加和减去哪个运算符?谢谢

2 个答案:

答案 0 :(得分:2)

这是:

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

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
  })
});

您只需将decrement替换为increment,即可decrement

有关详情: DO READ

答案 1 :(得分:0)

依次递增和递减

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

从序列化增量实例实例教程中采取的代码: http://docs.sequelizejs.com/manual/tutorial/instances.html