From 93d8426c84abdb4c89fa64961c00f3482fa194ff Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 21 Dec 2020 14:15:08 +0800 Subject: [PATCH] feat(database): add afterUpdateAssociations hook --- packages/database/src/database.ts | 6 +++--- packages/database/src/model.ts | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/database/src/database.ts b/packages/database/src/database.ts index 75bd693df3..84f8266906 100644 --- a/packages/database/src/database.ts +++ b/packages/database/src/database.ts @@ -264,7 +264,7 @@ export default class Database { * @param hookType * @param fn */ - public addHook(hookType: HookType, fn: Function) { + public addHook(hookType: HookType | string, fn: Function) { const hooks = this.hooks[hookType] || []; hooks.push(fn); this.hooks[hookType] = hooks; @@ -276,11 +276,11 @@ export default class Database { * @param hookType * @param args */ - public runHooks(hookType: HookType, ...args) { + public async runHooks(hookType: HookType | string, ...args) { const hooks = this.hooks[hookType] || []; for (const hook of hooks) { if (typeof hook === 'function') { - hook(...args); + await hook(...args); } } } diff --git a/packages/database/src/model.ts b/packages/database/src/model.ts index 86e0ccd5a5..dfa8c94172 100644 --- a/packages/database/src/model.ts +++ b/packages/database/src/model.ts @@ -511,6 +511,13 @@ export abstract class Model extends SequelizeModel { }); } + await this.database.runHooks('afterUpdateAssociations', this, { + ...options, + transaction, + }); + // @ts-ignore + // await this.sequelize.runHooks('afterUpdateAssociations', this, options); + if (!options.transaction) { await transaction.commit(); }