mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 03:55:10 +00:00
07c373956c
* chore: upgrade sequelize version to to latest * fix: type error * chore: type ModelCtor to ModelStatic * feat: update sequelize version of the map plugin * chore: update lock Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: chareice <chareice@live.com>
73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
import lodash from 'lodash';
|
|
import { SequelizeHooks } from 'sequelize/types/hooks';
|
|
|
|
import Database from './database';
|
|
import { Model } from './model';
|
|
|
|
const { hooks } = require('sequelize/lib/hooks');
|
|
|
|
export class ModelHook {
|
|
database: Database;
|
|
|
|
boundEvents = new Set<string>();
|
|
|
|
constructor(database: Database) {
|
|
this.database = database;
|
|
}
|
|
|
|
match(event: string | Symbol): keyof SequelizeHooks | null {
|
|
// NOTE: skip Symbol event
|
|
if (!lodash.isString(event)) {
|
|
return null;
|
|
}
|
|
|
|
const type = event.split('.').pop();
|
|
|
|
return type in hooks ? <keyof SequelizeHooks>type : null;
|
|
}
|
|
|
|
findModelName(hookArgs) {
|
|
for (const arg of hookArgs) {
|
|
if (arg?._previousDataValues) {
|
|
return (<Model>arg).constructor.name;
|
|
}
|
|
if (lodash.isPlainObject(arg)) {
|
|
if (arg['model']) {
|
|
return arg['model'].name;
|
|
}
|
|
const plural = arg?.name?.plural;
|
|
if (this.database.sequelize.isDefined(plural)) {
|
|
return plural;
|
|
}
|
|
const singular = arg?.name?.singular;
|
|
if (this.database.sequelize.isDefined(singular)) {
|
|
return singular;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
bindEvent(type) {
|
|
this.boundEvents.add(type);
|
|
}
|
|
|
|
hasBoundEvent(type): boolean {
|
|
return this.boundEvents.has(type);
|
|
}
|
|
|
|
buildSequelizeHook(type) {
|
|
return async (...args: any[]) => {
|
|
const modelName = this.findModelName(args);
|
|
|
|
if (modelName) {
|
|
// emit model event
|
|
await this.database.emitAsync(`${modelName}.${type}`, ...args);
|
|
}
|
|
|
|
// emit sequelize global event
|
|
await this.database.emitAsync(type, ...args);
|
|
};
|
|
}
|
|
}
|