Sequelize Hooks- Previous data values for afterBulkUpdate

巧了我就是萌 提交于 2021-01-28 14:42:08

问题


I am trying to create a hook beforeBulkUpdate and get new and previous values of a field. I tried using .reload() on the model and _previousDataValues but they only work on instances and not bulk create and update (I'm using bulk update). Is there a way to get the previous value of field using the beforeBulkUpdate hook?

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

回答1:


Solved it by passing individualHooks:true to the call.

Model.update({id: input.id}, { individualHooks: true});

And:

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


来源:https://stackoverflow.com/questions/53255911/sequelize-hooks-previous-data-values-for-afterbulkupdate

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