问题
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