feat: remove method
This commit is contained in:
		
							parent
							
								
									05fe32fcf1
								
							
						
					
					
						commit
						6013b4d274
					
				@ -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]
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -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',
 | 
			
		||||
        },
 | 
			
		||||
      ],
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -370,6 +370,7 @@ export default class UiSchemaRepository extends Repository {
 | 
			
		||||
      }
 | 
			
		||||
      return insertedNodes;
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
      console.log({ err });
 | 
			
		||||
      if (handleTransaction) {
 | 
			
		||||
        await transaction.rollback();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,3 @@
 | 
			
		||||
const hooks = [require('./removeEmptyParents').default];
 | 
			
		||||
const hooks = [require('./removeEmptyParents').default, require('./remove').default];
 | 
			
		||||
 | 
			
		||||
export { hooks };
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
@ -78,7 +78,7 @@ export class ServerHooks {
 | 
			
		||||
      {
 | 
			
		||||
        type: 'onCollectionFieldDestroy',
 | 
			
		||||
        collection: collectionName,
 | 
			
		||||
        'fields.$anyOf': [fieldName],
 | 
			
		||||
        field: fieldName,
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        collectionFieldInstance: fieldModel,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user