From 41832bbaf135a8f6ebc5b2852ffe4adf669924b5 Mon Sep 17 00:00:00 2001 From: Hemachandar <132386067+hmacr@users.noreply.github.com> Date: Tue, 31 Dec 2024 05:41:11 +0530 Subject: [PATCH] fix: Use parent transaction for findOrCreate after-commit hook (#8173) --- server/models/Event.ts | 7 ++++++- server/typings/sequelize.d.ts | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 server/typings/sequelize.d.ts diff --git a/server/models/Event.ts b/server/models/Event.ts index 392fa51a0a..9538e0acd0 100644 --- a/server/models/Event.ts +++ b/server/models/Event.ts @@ -83,7 +83,12 @@ class Event extends IdModel< options: SaveOptions> ) { if (options.transaction) { - options.transaction.afterCommit(() => void globalEventQueue.add(model)); + // 'findOrCreate' creates a new transaction always, and the transaction from the middleware is set as its parent. + // We want to use the parent transaction, otherwise the 'afterCommit' hook will never fire in this case. + // See: https://github.com/sequelize/sequelize/issues/17452 + (options.transaction.parent || options.transaction).afterCommit( + () => void globalEventQueue.add(model) + ); return; } void globalEventQueue.add(model); diff --git a/server/typings/sequelize.d.ts b/server/typings/sequelize.d.ts new file mode 100644 index 0000000000..ef994155ea --- /dev/null +++ b/server/typings/sequelize.d.ts @@ -0,0 +1,7 @@ +import "sequelize"; + +declare module "sequelize" { + interface Transaction { + parent?: Transaction; + } +}