From 34f33844d5c66ef14433164c3cd04bd3f0f90573 Mon Sep 17 00:00:00 2001 From: Chareice Date: Thu, 10 Feb 2022 20:28:27 +0800 Subject: [PATCH] feat: removeSchema Hook with params --- .../src/__tests__/server-hook-impl.test.ts | 16 ++++-- .../src/repository.ts | 48 ++++++++++++++++ .../src/server-hooks/hooks/index.ts | 8 ++- .../src/server-hooks/hooks/remove.ts | 9 --- .../server-hooks/hooks/removeEmptyParents.ts | 55 ------------------- .../src/server-hooks/index.ts | 7 ++- 6 files changed, 73 insertions(+), 70 deletions(-) delete mode 100644 packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove.ts delete mode 100644 packages/plugin-ui-schema-storage/src/server-hooks/hooks/removeEmptyParents.ts 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 8e3499fd77..fed15cd2b6 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 @@ -92,7 +92,7 @@ describe('server hooks', () => { type: 'onCollectionFieldDestroy', collection: 'posts', field: 'name', - method: 'removeEmptyParents', + method: 'removeSchema', }, ], }, @@ -108,7 +108,11 @@ describe('server hooks', () => { type: 'onCollectionFieldDestroy', collection: 'posts', field: 'title', - method: 'removeEmptyParents', + method: 'removeSchema', + params: { + breakComponent: 'Grid', + removeEmptyParents: true, + }, }, ], }, @@ -130,7 +134,11 @@ describe('server hooks', () => { type: 'onCollectionFieldDestroy', collection: 'posts', field: 'intro', - method: 'removeEmptyParents', + method: 'removeSchema', + params: { + breakComponent: 'Grid', + removeEmptyParents: true, + }, }, ], }, @@ -184,7 +192,7 @@ describe('server hooks', () => { { type: 'onCollectionDestroy', collection: 'posts', - method: 'remove', + method: 'removeSchema', }, ], }, diff --git a/packages/plugin-ui-schema-storage/src/repository.ts b/packages/plugin-ui-schema-storage/src/repository.ts index 58b6346e4e..9efd51930e 100644 --- a/packages/plugin-ui-schema-storage/src/repository.ts +++ b/packages/plugin-ui-schema-storage/src/repository.ts @@ -227,6 +227,54 @@ export default class UiSchemaRepository extends Repository { ); } + protected async isSingleChild(uid, transaction) { + const db = this.database; + + const parent = await db.getRepository('ui_schema_tree_path').findOne({ + filter: { + descendant: uid, + depth: 1, + }, + }); + + const countResult = await db.sequelize.query( + `SELECT COUNT(*) FROM ${ + db.getCollection('ui_schema_tree_path').model.tableName + } where ancestor = :ancestor and depth = 1`, + { + replacements: { + ancestor: parent.get('ancestor'), + }, + type: 'SELECT', + transaction, + }, + ); + + const parentChildrenCount = countResult[0]['count']; + + if (parentChildrenCount == 1) { + return parent.get('ancestor') as string; + } + + return null; + } + + async removeEmptyParents(options: TransactionAble & { uid: string; breakComponent?: string }) { + const { transaction, uid, breakComponent } = options; + + const removeParent = async (nodeUid: string) => { + const parentUid = await this.isSingleChild(nodeUid, transaction); + + if (parentUid) { + await removeParent(parentUid); + } else { + await this.remove(nodeUid, { transaction }); + } + }; + + await removeParent(uid); + } + async remove(uid: string, options?: TransactionAble) { let handleTransaction: boolean = true; let transaction; 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 0852264bcd..3d3e02b1d2 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,9 @@ -const hooks = [require('./removeEmptyParents').default, require('./remove').default]; +import { hookFactory } from './factory'; +import { removeSchema } from './removeSchema'; + +const hooks = [ + hookFactory('onCollectionDestroy', 'removeSchema', removeSchema), + hookFactory('onCollectionFieldDestroy', 'removeSchema', removeSchema), +]; 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 deleted file mode 100644 index cfaff488a6..0000000000 --- a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove.ts +++ /dev/null @@ -1,9 +0,0 @@ -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/hooks/removeEmptyParents.ts b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/removeEmptyParents.ts deleted file mode 100644 index 4f96776c11..0000000000 --- a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/removeEmptyParents.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage'; -import { hookFactory } from './factory'; - -// given a child uid, if it is a single child ,return its parent -async function isSingleChild(uid, db, transaction) { - const parent = await db.getRepository('ui_schema_tree_path').findOne({ - filter: { - descendant: uid, - depth: 1, - }, - }); - - const countResult = await db.sequelize.query( - `SELECT COUNT(*) FROM ${ - db.getCollection('ui_schema_tree_path').model.tableName - } where ancestor = :ancestor and depth = 1`, - { - replacements: { - ancestor: parent.get('ancestor'), - }, - type: 'SELECT', - transaction, - }, - ); - - const parentChildrenCount = countResult[0]['count']; - - if (parentChildrenCount == 1) { - return parent.get('ancestor'); - } - - return null; -} - -async function removeEmptyParents({ schemaInstance, options, db }) { - const { transaction } = options; - const uiSchemaRepository: UiSchemaRepository = db.getRepository('ui_schemas'); - const uid = schemaInstance.get('uid'); - - // find parent uid - const parentUid = await isSingleChild(uid, db, transaction); - - if (parentUid) { - const rowUid = await isSingleChild(parentUid, db, transaction); - if (rowUid) { - await uiSchemaRepository.remove(rowUid, { transaction }); - } else { - await uiSchemaRepository.remove(parentUid, { transaction }); - } - } else { - await uiSchemaRepository.remove(uid, { transaction }); - } -} - -export default hookFactory('onCollectionFieldDestroy', 'removeEmptyParents', removeEmptyParents); 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 cb27986360..00056954b8 100644 --- a/packages/plugin-ui-schema-storage/src/server-hooks/index.ts +++ b/packages/plugin-ui-schema-storage/src/server-hooks/index.ts @@ -116,7 +116,12 @@ export class ServerHooks { const hookFunc = this.hooks.get(hookRecord.get('type') as HookType)?.get(hoodMethodName); if (hookFunc) { - await hookFunc({ ...hooksArgs, schemaInstance: (hookRecord).uiSchema, db: this.db }); + await hookFunc({ + ...hooksArgs, + schemaInstance: (hookRecord).uiSchema, + db: this.db, + params: hookRecord.get('params'), + }); } } }