diff --git a/packages/database/src/repository.ts b/packages/database/src/repository.ts index 7290d6d35..6f229fd5a 100644 --- a/packages/database/src/repository.ts +++ b/packages/database/src/repository.ts @@ -387,6 +387,10 @@ export class Repositoryoptions; + if (options['individualHooks'] === undefined) { + options['individualHooks'] = true; + } + const filterByTk: TargetKey[] | undefined = options.filterByTk && !lodash.isArray(options.filterByTk) ? [options.filterByTk] diff --git a/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts index 895a71570..8e3499fd7 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts @@ -91,7 +91,7 @@ describe('server hooks', () => { { type: 'onCollectionFieldDestroy', collection: 'posts', - fields: ['name'], + field: 'name', method: 'removeEmptyParents', }, ], @@ -107,7 +107,7 @@ describe('server hooks', () => { { type: 'onCollectionFieldDestroy', collection: 'posts', - fields: ['title'], + field: 'title', method: 'removeEmptyParents', }, ], @@ -129,7 +129,7 @@ describe('server hooks', () => { { type: 'onCollectionFieldDestroy', collection: 'posts', - fields: ['intro'], + field: 'intro', method: 'removeEmptyParents', }, ], @@ -172,6 +172,7 @@ describe('server hooks', () => { const schema = { 'x-uid': 'root', + name: 'root', properties: { child1: { 'x-uid': 'child1', @@ -179,9 +180,27 @@ describe('server hooks', () => { child2: { 'x-uid': 'child2', - 'x-server-hooks': [], + 'x-server-hooks': [ + { + type: 'onCollectionDestroy', + collection: 'posts', + method: 'remove', + }, + ], }, }, }; + + await uiSchemaRepository.insert(schema); + + await db.getRepository('collections').destroy({ + filter: { + name: 'posts', + }, + }); + + const jsonTree = await uiSchemaRepository.getJsonSchema('root'); + expect(jsonTree['properties']['child1']).toBeDefined(); + expect(jsonTree['properties']['child2']).not.toBeDefined(); }); }); 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 50be924d5..814dcbf06 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 @@ -37,7 +37,7 @@ describe('server hooks', () => { { type: 'onCollectionFieldDestroy', collection: 'posts', - fields: ['title'], + field: 'title', method: 'onFieldDestroy', }, ], @@ -221,7 +221,7 @@ describe('server hooks', () => { { type: 'onCollectionFieldDestroy', collection: 'posts', - fields: ['title'], + field: 'title', method: 'preventDestroy', }, ], diff --git a/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts b/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts index 9fe75eb1b..3f0b6a3fe 100644 --- a/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts +++ b/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts @@ -6,19 +6,23 @@ export default { // autoGenId: false, timestamps: false, fields: [ - { type: 'belongsTo', name: 'uiSchema', target: 'ui_schemas', foreignKey: 'uid' }, + { type: 'belongsTo', name: 'uiSchema', target: 'ui_schemas', foreignKey: 'uid', onDelete: 'CASCADE' }, { type: 'string', name: 'type' }, { type: 'string', name: 'collection', }, { - type: 'array', - name: 'fields', + type: 'string', + name: 'field', }, { type: 'string', name: 'method', }, + { + type: 'json', + name: 'params', + }, ], } as CollectionOptions; diff --git a/packages/plugin-ui-schema-storage/src/repository.ts b/packages/plugin-ui-schema-storage/src/repository.ts index 08b0f0241..58b6346e4 100644 --- a/packages/plugin-ui-schema-storage/src/repository.ts +++ b/packages/plugin-ui-schema-storage/src/repository.ts @@ -370,6 +370,7 @@ export default class UiSchemaRepository extends Repository { } return insertedNodes; } catch (err) { + console.log({ err }); if (handleTransaction) { await transaction.rollback(); } diff --git a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/index.ts b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/index.ts index e378e501f..0852264bc 100644 --- a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/index.ts +++ b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/index.ts @@ -1,3 +1,3 @@ -const hooks = [require('./removeEmptyParents').default]; +const hooks = [require('./removeEmptyParents').default, require('./remove').default]; export { hooks }; diff --git a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove.ts b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove.ts new file mode 100644 index 000000000..cfaff488a --- /dev/null +++ b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove.ts @@ -0,0 +1,9 @@ +import { hookFactory } from './factory'; +import { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage'; + +async function remove({ schemaInstance, options, db }) { + const uiSchemaRepository: UiSchemaRepository = db.getRepository('ui_schemas'); + await uiSchemaRepository.remove(schemaInstance.get('uid')); +} + +export default hookFactory('onCollectionDestroy', 'remove', remove); 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 532aa5f97..cb2798636 100644 --- a/packages/plugin-ui-schema-storage/src/server-hooks/index.ts +++ b/packages/plugin-ui-schema-storage/src/server-hooks/index.ts @@ -78,7 +78,7 @@ export class ServerHooks { { type: 'onCollectionFieldDestroy', collection: collectionName, - 'fields.$anyOf': [fieldName], + field: fieldName, }, { collectionFieldInstance: fieldModel,