diff --git a/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts index 9e5c40702..3462f71f0 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts @@ -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(); + }); }); diff --git a/packages/plugin-ui-schema-storage/src/server-hooks/index.ts b/packages/plugin-ui-schema-storage/src/server-hooks/index.ts index 4ab0b4253..347f066d2 100644 --- a/packages/plugin-ui-schema-storage/src/server-hooks/index.ts +++ b/packages/plugin-ui-schema-storage/src/server-hooks/index.ts @@ -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>(); @@ -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, diff --git a/packages/plugin-ui-schema-storage/src/server.ts b/packages/plugin-ui-schema-storage/src/server.ts index 139bf17d9..5cedd7fdb 100644 --- a/packages/plugin-ui-schema-storage/src/server.ts +++ b/packages/plugin-ui-schema-storage/src/server.ts @@ -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')); });