2024-04-30 07:51:31 +00:00
|
|
|
/**
|
|
|
|
* This file is part of the NocoBase (R) project.
|
|
|
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
|
|
* Authors: NocoBase Team.
|
|
|
|
*
|
|
|
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
|
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
|
|
*/
|
|
|
|
|
2021-12-06 13:12:54 +00:00
|
|
|
import lodash from 'lodash';
|
2022-12-24 08:30:01 +00:00
|
|
|
import { SequelizeHooks } from 'sequelize/types/hooks';
|
|
|
|
|
2022-02-17 08:16:05 +00:00
|
|
|
import Database from './database';
|
2022-02-23 10:18:38 +00:00
|
|
|
import { Model } from './model';
|
2021-12-06 13:12:54 +00:00
|
|
|
|
|
|
|
const { hooks } = require('sequelize/lib/hooks');
|
|
|
|
|
|
|
|
export class ModelHook {
|
|
|
|
database: Database;
|
2022-06-08 03:22:08 +00:00
|
|
|
|
|
|
|
boundEvents = new Set<string>();
|
2021-12-06 13:12:54 +00:00
|
|
|
|
|
|
|
constructor(database: Database) {
|
|
|
|
this.database = database;
|
|
|
|
}
|
|
|
|
|
2023-04-25 05:12:14 +00:00
|
|
|
match(event: string | symbol): keyof SequelizeHooks | null {
|
2022-06-08 03:22:08 +00:00
|
|
|
// NOTE: skip Symbol event
|
|
|
|
if (!lodash.isString(event)) {
|
|
|
|
return null;
|
2021-12-06 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 03:22:08 +00:00
|
|
|
const type = event.split('.').pop();
|
|
|
|
|
|
|
|
return type in hooks ? <keyof SequelizeHooks>type : null;
|
2021-12-06 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
findModelName(hookArgs) {
|
|
|
|
for (const arg of hookArgs) {
|
2022-04-29 13:42:54 +00:00
|
|
|
if (arg?._previousDataValues) {
|
2021-12-06 13:12:54 +00:00
|
|
|
return (<Model>arg).constructor.name;
|
|
|
|
}
|
2022-03-14 05:24:00 +00:00
|
|
|
if (lodash.isPlainObject(arg)) {
|
|
|
|
if (arg['model']) {
|
|
|
|
return arg['model'].name;
|
|
|
|
}
|
2024-06-18 07:31:45 +00:00
|
|
|
|
|
|
|
const modelName = arg['modelName'];
|
|
|
|
if (this.database.sequelize.isDefined(modelName)) {
|
|
|
|
return modelName;
|
2022-03-14 05:24:00 +00:00
|
|
|
}
|
2021-12-06 13:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-08 03:22:08 +00:00
|
|
|
bindEvent(type) {
|
|
|
|
this.boundEvents.add(type);
|
2021-12-06 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 03:22:08 +00:00
|
|
|
hasBoundEvent(type): boolean {
|
|
|
|
return this.boundEvents.has(type);
|
2021-12-06 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 03:22:08 +00:00
|
|
|
buildSequelizeHook(type) {
|
2021-12-06 13:12:54 +00:00
|
|
|
return async (...args: any[]) => {
|
|
|
|
const modelName = this.findModelName(args);
|
2022-06-14 07:46:48 +00:00
|
|
|
|
2021-12-06 13:12:54 +00:00
|
|
|
if (modelName) {
|
|
|
|
// emit model event
|
2022-06-08 03:22:08 +00:00
|
|
|
await this.database.emitAsync(`${modelName}.${type}`, ...args);
|
2021-12-06 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// emit sequelize global event
|
2022-06-08 03:22:08 +00:00
|
|
|
await this.database.emitAsync(type, ...args);
|
2021-12-06 13:12:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|