feat: removeSchema Hook with params
This commit is contained in:
parent
6013b4d274
commit
34f33844d5
@ -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',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
@ -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;
|
||||
|
@ -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 };
|
||||
|
@ -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);
|
@ -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);
|
@ -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: (<any>hookRecord).uiSchema, db: this.db });
|
||||
await hookFunc({
|
||||
...hooksArgs,
|
||||
schemaInstance: (<any>hookRecord).uiSchema,
|
||||
db: this.db,
|
||||
params: hookRecord.get('params'),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user