You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/transactions.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,3 +219,45 @@ sequelize.transaction({
219
219
The `transaction` option goes with most other options, which are usually the first argument of a method.
220
220
For methods that take values, like `.create`, `.update()`, `.updateAttributes()` etc. `transaction` should be passed to the option in the second argument.
221
221
If unsure, refer to the API documentation for the method you are using to be sure of the signature.
222
+
223
+
## After commit hook
224
+
225
+
A `transaction` object allows tracking if and when it is committed.
226
+
227
+
An `afterCommit` hook can be added to both managed and unmanaged transaction objects:
228
+
```js
229
+
sequelize.transaction(t=> {
230
+
t.afterCommit((transaction) => {
231
+
// Your logic
232
+
});
233
+
});
234
+
235
+
sequelize.transaction().then(t=> {
236
+
t.afterCommit((transaction) => {
237
+
// Your logic
238
+
});
239
+
240
+
returnt.commit();
241
+
})
242
+
```
243
+
244
+
The function passed to `afterCommit` can optionally return a promise that will resolve before the promise chain
245
+
that created the transaction resolves
246
+
247
+
`afterCommit` hooks are _not_ raised if a transaction is rolled back
248
+
249
+
`afterCommit` hooks do _not_ modify the return value of the transaction, unlike standard hooks
250
+
251
+
You can use the `afterCommit` hook in conjunction with model hooks to know when a instance is saved and available outside
252
+
of a transaction
253
+
254
+
```js
255
+
model.afterSave((instance, options) => {
256
+
if (options.transaction) {
257
+
// Save done within a transaction, wait until transaction is committed to
0 commit comments