feat: server hooks afterDestroyCollection && afterCreateSelf
This commit is contained in:
parent
3e1cad643b
commit
41456b562e
@ -18,7 +18,7 @@ describe('server hooks', () => {
|
||||
'x-component': 'Table',
|
||||
'x-collection': 'posts',
|
||||
'x-server-hooks': {
|
||||
afterDestroyCollection: ['aaa'],
|
||||
afterDestroyCollection: ['onCollectionDestroy'],
|
||||
},
|
||||
properties: {
|
||||
col1: {
|
||||
@ -85,7 +85,7 @@ describe('server hooks', () => {
|
||||
expect(nodeFieldAttr.get('collectionPath')).toEqual('posts.title');
|
||||
});
|
||||
|
||||
it('should call server hooks', async () => {
|
||||
it('should call server hooks afterFieldDestroy', async () => {
|
||||
const PostModel = await db.getRepository('collections').create({
|
||||
values: {
|
||||
name: 'posts',
|
||||
@ -118,4 +118,61 @@ describe('server hooks', () => {
|
||||
|
||||
expect(hookFn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call server hooks afterCollectionDestroy', async () => {
|
||||
const PostModel = await db.getRepository('collections').create({
|
||||
values: {
|
||||
name: 'posts',
|
||||
},
|
||||
});
|
||||
|
||||
const fieldModel = await db.getRepository('fields').create({
|
||||
values: {
|
||||
name: 'title',
|
||||
type: 'string',
|
||||
collectionName: 'posts',
|
||||
},
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
await PostModel.migrate();
|
||||
|
||||
const serverHooks = uiSchemaPlugin.serverHooks;
|
||||
|
||||
const hookFn = jest.fn();
|
||||
|
||||
serverHooks.register('afterDestroyCollection', 'onCollectionDestroy', hookFn);
|
||||
|
||||
// destroy a field
|
||||
await db.getRepository('collections').destroy({
|
||||
filter: {
|
||||
name: 'posts',
|
||||
},
|
||||
individualHooks: true,
|
||||
});
|
||||
|
||||
expect(hookFn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call server hooks afterUiSchemaCreated', async () => {
|
||||
const menuSchema = {
|
||||
'x-uid': 'menu',
|
||||
'x-server-hooks': {
|
||||
afterCreateSelf: ['afterCreateMenu'],
|
||||
},
|
||||
};
|
||||
|
||||
const serverHooks = uiSchemaPlugin.serverHooks;
|
||||
const hookFn = jest.fn();
|
||||
|
||||
serverHooks.register('afterCreateSelf', 'afterCreateMenu', hookFn);
|
||||
|
||||
await uiSchemaRepository.create({
|
||||
values: {
|
||||
schema: menuSchema,
|
||||
},
|
||||
});
|
||||
|
||||
expect(hookFn).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Database } from '@nocobase/database';
|
||||
import { UiSchemaModel } from '../model';
|
||||
|
||||
export type HookType = 'afterDestroyField';
|
||||
export type HookType = 'afterDestroyField' | 'afterDestroyCollection' | 'afterCreateSelf';
|
||||
|
||||
export class ServerHooks {
|
||||
hooks = new Map<HookType, Map<string, any>>();
|
||||
@ -14,6 +14,53 @@ export class ServerHooks {
|
||||
this.db.on('fields.afterDestroy', async (model, options) => {
|
||||
await this.afterFieldDestroy(model, options);
|
||||
});
|
||||
|
||||
this.db.on('collections.afterDestroy', async (model, options) => {
|
||||
await this.afterCollectionDestroy(model, options);
|
||||
});
|
||||
|
||||
this.db.on('ui_schemas.afterCreate', async (model, options) => {
|
||||
await this.afterUiSchemaCreated(model, options);
|
||||
});
|
||||
}
|
||||
|
||||
protected async afterUiSchemaCreated(uiSchemaModel, options) {
|
||||
const { transaction } = options;
|
||||
const listenHooksName = uiSchemaModel.getListenServerHooks('afterCreateSelf');
|
||||
|
||||
for (const listenHookName of listenHooksName) {
|
||||
const hookFunc = this.hooks.get('afterCreateSelf')?.get(listenHookName);
|
||||
|
||||
await hookFunc({
|
||||
model: uiSchemaModel,
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected async afterCollectionDestroy(collectionModel, options) {
|
||||
const { transaction } = options;
|
||||
|
||||
const collectionPath = collectionModel.get('name');
|
||||
|
||||
const listenSchemas = (await this.db.getRepository('ui_schemas').find({
|
||||
filter: {
|
||||
'attrs.collectionPath': collectionPath,
|
||||
},
|
||||
transaction,
|
||||
})) as UiSchemaModel[];
|
||||
|
||||
for (const listenSchema of listenSchemas) {
|
||||
const listenHooksName = listenSchema.getListenServerHooks('afterDestroyCollection');
|
||||
for (const listenHookName of listenHooksName) {
|
||||
const hookFunc = this.hooks.get('afterDestroyCollection')?.get(listenHookName);
|
||||
|
||||
await hookFunc({
|
||||
model: listenSchema,
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async afterFieldDestroy(fieldModel, options) {
|
||||
@ -32,6 +79,7 @@ export class ServerHooks {
|
||||
const listenHooksName = listenSchema.getListenServerHooks('afterDestroyField');
|
||||
for (const listenHookName of listenHooksName) {
|
||||
const hookFunc = this.hooks.get('afterDestroyField')?.get(listenHookName);
|
||||
|
||||
await hookFunc({
|
||||
model: listenSchema,
|
||||
transaction,
|
||||
|
@ -24,10 +24,6 @@ export default class PluginUiSchema extends Plugin {
|
||||
|
||||
this.registerRepository();
|
||||
|
||||
db.on('ui_schemas.afterDefine', (model) => {
|
||||
model.primaryKeyAttribute = 'uid';
|
||||
});
|
||||
|
||||
db.on('ui_schemas.beforeCreate', function setUid(model) {
|
||||
model.set('uid', model.get('x-uid'));
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user