After commit hook
A transaction
object allows tracking if and when it is committed.
An afterCommit
hook can be added to both managed and unmanaged transaction objects:
sequelize.transaction(t => {
t.afterCommit((transaction) => {
// Your logic
});
});
sequelize.transaction().then(t => {
t.afterCommit((transaction) => {
// Your logic
});
return t.commit();
})
The function passed to afterCommit
can optionally return a promise that will resolve before the promise chainthat created the transaction resolves
afterCommit
hooks are not raised if a transaction is rolled back
afterCommit
hooks do not modify the return value of the transaction, unlike standard hooks
You can use the afterCommit
hook in conjunction with model hooks to know when a instance is saved and available outsideof a transaction
model.afterSave((instance, options) => {
if (options.transaction) {
// Save done within a transaction, wait until transaction is committed to
// notify listeners the instance has been saved
options.transaction.afterCommit(() => /* Notify */)
return;
}
// Save done outside a transaction, safe for callers to fetch the updated model
// Notify
})