Sequelize脏模型

时间:2017-08-30 22:12:06

标签: sequelize.js

sequelize是否有办法检查模型是否已更改?例如

var user = User.findOne(where:{email:'test@user.com'}).then(user=>{
   if(some_condition)user.email = 'changed@email.com'
   //can I do something like this to check if model has been changed? before the save action?
   if(user.dirty){}
})

1 个答案:

答案 0 :(得分:1)

Sequelize在模型实例级别确实有一个更改的方法,但是这会将当前值与_previousDataValues进行比较。

已记录here

可以用作

Model.testModel.findById(23)
.then(instance => {
   instance.name = "new Name";
   console.log(instance.changed());
   // This should log [name] and would be false if nothing was changed.
})