Sequelize Hooks - afterBulkUpdate 的先前数据值

nia*_*iaS 2 sequelize.js

我正在尝试创建一个钩子beforeBulkUpdate并获取字段的新值和先前值。我尝试.reload()在模型上使用,_previousDataValues但它们仅适用于实例,而不适用于批量创建和更新(我正在使用批量更新)。有没有办法使用beforeBulkUpdate钩子获取字段的先前值?

beforeBulkUpdate: (person) => {
  console.log(person.name) // 'John' (new name)
  Person.findOne({ where: { id: input.id } }).then(person => {
  person.reload().then(() => {
    console.log(person.name) // 'John' (expected old name, but returns new name)
    })
  })
}
Run Code Online (Sandbox Code Playgroud)

nia*_*iaS 11

通过传递individualHooks:true给调用解决了它。

Model.update({id: input.id}, { individualHooks: true});
Run Code Online (Sandbox Code Playgroud)

和:

hooks: {
      beforeUpdate: (instance, options) => {
        console.log(instance.dataValues); // new values
        console.log(instance._previousDataValues); // current values
      }
    }
Run Code Online (Sandbox Code Playgroud)