feat: remove method

This commit is contained in:
Chareice 2022-02-10 19:11:43 +08:00 committed by chenos
parent 05fe32fcf1
commit 6013b4d274
8 changed files with 48 additions and 11 deletions

View File

@ -387,6 +387,10 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
options = <DestroyOptions>options;
if (options['individualHooks'] === undefined) {
options['individualHooks'] = true;
}
const filterByTk: TargetKey[] | undefined =
options.filterByTk && !lodash.isArray(options.filterByTk)
? [options.filterByTk]

View File

@ -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();
});
});

View File

@ -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',
},
],

View File

@ -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;

View File

@ -370,6 +370,7 @@ export default class UiSchemaRepository extends Repository {
}
return insertedNodes;
} catch (err) {
console.log({ err });
if (handleTransaction) {
await transaction.rollback();
}

View File

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

View File

@ -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);

View File

@ -78,7 +78,7 @@ export class ServerHooks {
{
type: 'onCollectionFieldDestroy',
collection: collectionName,
'fields.$anyOf': [fieldName],
field: fieldName,
},
{
collectionFieldInstance: fieldModel,