feat(database): add afterUpdateAssociations hook

This commit is contained in:
chenos 2020-12-21 14:15:08 +08:00
parent fe8476c5dd
commit 93d8426c84
2 changed files with 10 additions and 3 deletions

View File

@ -264,7 +264,7 @@ export default class Database {
* @param hookType * @param hookType
* @param fn * @param fn
*/ */
public addHook(hookType: HookType, fn: Function) { public addHook(hookType: HookType | string, fn: Function) {
const hooks = this.hooks[hookType] || []; const hooks = this.hooks[hookType] || [];
hooks.push(fn); hooks.push(fn);
this.hooks[hookType] = hooks; this.hooks[hookType] = hooks;
@ -276,11 +276,11 @@ export default class Database {
* @param hookType * @param hookType
* @param args * @param args
*/ */
public runHooks(hookType: HookType, ...args) { public async runHooks(hookType: HookType | string, ...args) {
const hooks = this.hooks[hookType] || []; const hooks = this.hooks[hookType] || [];
for (const hook of hooks) { for (const hook of hooks) {
if (typeof hook === 'function') { if (typeof hook === 'function') {
hook(...args); await hook(...args);
} }
} }
} }

View File

@ -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) { if (!options.transaction) {
await transaction.commit(); await transaction.commit();
} }