feat: register default hooks

This commit is contained in:
Chareice 2022-02-10 13:58:26 +08:00 committed by chenos
parent 4607e0da49
commit 05fe32fcf1
6 changed files with 54 additions and 6 deletions

View File

@ -2,7 +2,6 @@ import { mockServer, MockServer } from '@nocobase/test';
import { Database } from '@nocobase/database';
import PluginUiSchema, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import PluginCollectionManager from '@nocobase/plugin-collection-manager';
import { removeEmptyParents } from '../server-hooks/removeEmptyParents';
describe('server hooks', () => {
let app: MockServer;
@ -144,8 +143,6 @@ describe('server hooks', () => {
await uiSchemaRepository.insert(schema);
uiSchemaPlugin.serverHooks.register('onCollectionFieldDestroy', 'removeEmptyParents', removeEmptyParents);
await db.getRepository('fields').destroy({
filter: {
name: 'intro',
@ -157,4 +154,34 @@ describe('server hooks', () => {
expect(jsonTree['properties']['row1']['properties']['col11']).toBeDefined();
expect(jsonTree['properties']['row1']['properties']['col12']).not.toBeDefined();
});
it('should remove schema when collection destroy', async () => {
await db.getRepository('collections').create({
values: {
name: 'posts',
},
});
await db.getRepository('fields').create({
values: {
name: 'title',
type: 'string',
collectionName: 'posts',
},
});
const schema = {
'x-uid': 'root',
properties: {
child1: {
'x-uid': 'child1',
},
child2: {
'x-uid': 'child2',
'x-server-hooks': [],
},
},
};
});
});

View File

@ -1,4 +1,4 @@
import { Repository } from '@nocobase/database';
import { Repository, TransactionAble } from '@nocobase/database';
import lodash from 'lodash';
import { ChildOptions, SchemaNode, TargetPosition, UiSchemaNodeDAO } from './dao/ui_schema_node_dao';
import { uid } from '@nocobase/utils';
@ -227,7 +227,7 @@ export default class UiSchemaRepository extends Repository {
);
}
async remove(uid: string, options?) {
async remove(uid: string, options?: TransactionAble) {
let handleTransaction: boolean = true;
let transaction;

View File

@ -0,0 +1,9 @@
import { HookType } from '../index';
export function hookFactory(hookType: HookType, hookName: string, hookFunc) {
return {
hookType,
hookName,
hookFunc,
};
}

View File

@ -0,0 +1,3 @@
const hooks = [require('./removeEmptyParents').default];
export { hooks };

View File

@ -1,4 +1,5 @@
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) {
@ -31,7 +32,7 @@ async function isSingleChild(uid, db, transaction) {
return null;
}
export async function removeEmptyParents({ schemaInstance, options, db }) {
async function removeEmptyParents({ schemaInstance, options, db }) {
const { transaction } = options;
const uiSchemaRepository: UiSchemaRepository = db.getRepository('ui_schemas');
const uid = schemaInstance.get('uid');
@ -50,3 +51,5 @@ export async function removeEmptyParents({ schemaInstance, options, db }) {
await uiSchemaRepository.remove(uid, { transaction });
}
}
export default hookFactory('onCollectionFieldDestroy', 'removeEmptyParents', removeEmptyParents);

View File

@ -1,5 +1,6 @@
import { Database } from '@nocobase/database';
import { ServerHookModel } from './model';
import { hooks } from './hooks';
export type HookType =
| 'onSelfDestroy'
@ -13,6 +14,11 @@ export class ServerHooks {
constructor(protected db: Database) {
this.listen();
this.registerHooks();
}
registerHooks() {
hooks.forEach((hook) => this.register(hook.hookType, hook.hookName, hook.hookFunc));
}
listen() {