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; options = <DestroyOptions>options;
if (options['individualHooks'] === undefined) {
options['individualHooks'] = true;
}
const filterByTk: TargetKey[] | undefined = const filterByTk: TargetKey[] | undefined =
options.filterByTk && !lodash.isArray(options.filterByTk) options.filterByTk && !lodash.isArray(options.filterByTk)
? [options.filterByTk] ? [options.filterByTk]

View File

@ -91,7 +91,7 @@ describe('server hooks', () => {
{ {
type: 'onCollectionFieldDestroy', type: 'onCollectionFieldDestroy',
collection: 'posts', collection: 'posts',
fields: ['name'], field: 'name',
method: 'removeEmptyParents', method: 'removeEmptyParents',
}, },
], ],
@ -107,7 +107,7 @@ describe('server hooks', () => {
{ {
type: 'onCollectionFieldDestroy', type: 'onCollectionFieldDestroy',
collection: 'posts', collection: 'posts',
fields: ['title'], field: 'title',
method: 'removeEmptyParents', method: 'removeEmptyParents',
}, },
], ],
@ -129,7 +129,7 @@ describe('server hooks', () => {
{ {
type: 'onCollectionFieldDestroy', type: 'onCollectionFieldDestroy',
collection: 'posts', collection: 'posts',
fields: ['intro'], field: 'intro',
method: 'removeEmptyParents', method: 'removeEmptyParents',
}, },
], ],
@ -172,6 +172,7 @@ describe('server hooks', () => {
const schema = { const schema = {
'x-uid': 'root', 'x-uid': 'root',
name: 'root',
properties: { properties: {
child1: { child1: {
'x-uid': 'child1', 'x-uid': 'child1',
@ -179,9 +180,27 @@ describe('server hooks', () => {
child2: { child2: {
'x-uid': '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', type: 'onCollectionFieldDestroy',
collection: 'posts', collection: 'posts',
fields: ['title'], field: 'title',
method: 'onFieldDestroy', method: 'onFieldDestroy',
}, },
], ],
@ -221,7 +221,7 @@ describe('server hooks', () => {
{ {
type: 'onCollectionFieldDestroy', type: 'onCollectionFieldDestroy',
collection: 'posts', collection: 'posts',
fields: ['title'], field: 'title',
method: 'preventDestroy', method: 'preventDestroy',
}, },
], ],

View File

@ -6,19 +6,23 @@ export default {
// autoGenId: false, // autoGenId: false,
timestamps: false, timestamps: false,
fields: [ 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: 'type' },
{ {
type: 'string', type: 'string',
name: 'collection', name: 'collection',
}, },
{ {
type: 'array', type: 'string',
name: 'fields', name: 'field',
}, },
{ {
type: 'string', type: 'string',
name: 'method', name: 'method',
}, },
{
type: 'json',
name: 'params',
},
], ],
} as CollectionOptions; } as CollectionOptions;

View File

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

View File

@ -1,3 +1,3 @@
const hooks = [require('./removeEmptyParents').default]; const hooks = [require('./removeEmptyParents').default, require('./remove').default];
export { hooks }; 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', type: 'onCollectionFieldDestroy',
collection: collectionName, collection: collectionName,
'fields.$anyOf': [fieldName], field: fieldName,
}, },
{ {
collectionFieldInstance: fieldModel, collectionFieldInstance: fieldModel,