From dfcf7671d35ab9b2dbf6088f5ccb5716f0c4d72e Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 7 Apr 2024 13:14:48 +0800 Subject: [PATCH] feat: getParentJsonSchema in ui schema repository (#3690) * feat: getParentJsonSchema in ui schema repository * chore: ui schema snippet * chore: method name * chore: test * chore: test * fix: getParentProperty --------- Co-authored-by: chenos --- .../src/server/__tests__/action.test.ts | 31 ++++++++++++- .../__tests__/ui-schema-repository.test.ts | 44 ++++++++++++++++++- .../src/server/actions/ui-schema-action.ts | 4 ++ .../src/server/repository.ts | 20 +++++++++ .../src/server/server.ts | 15 +------ 5 files changed, 98 insertions(+), 16 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/action.test.ts b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/action.test.ts index fe35689c23..73ff07097f 100644 --- a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/action.test.ts +++ b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/action.test.ts @@ -1,5 +1,5 @@ import { Database } from '@nocobase/database'; -import { MockServer, createMockServer } from '@nocobase/test'; +import { createMockServer, MockServer } from '@nocobase/test'; describe('action test', () => { let app: MockServer; @@ -17,6 +17,35 @@ describe('action test', () => { await app.destroy(); }); + test('get parent property', async () => { + await app + .agent() + .resource('uiSchemas') + .insert({ + values: { + 'x-uid': 'n1', + name: 'a', + type: 'object', + properties: { + b: { + 'x-uid': 'n2', + type: 'object', + properties: { + c: { 'x-uid': 'n3' }, + }, + }, + d: { 'x-uid': 'n4' }, + }, + }, + }); + + const response = await app.agent().resource('uiSchemas').getParentProperty({ + filterByTk: 'n2', + }); + + expect(response.body.data['x-uid']).toEqual('n1'); + }); + test('insert action', async () => { const response = await app .agent() diff --git a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/ui-schema-repository.test.ts b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/ui-schema-repository.test.ts index 00a6aae250..53b73202a5 100644 --- a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/ui-schema-repository.test.ts +++ b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/__tests__/ui-schema-repository.test.ts @@ -1,5 +1,5 @@ import { Collection, Database } from '@nocobase/database'; -import { createMockServer, MockServer } from '@nocobase/test'; +import { MockServer, createMockServer } from '@nocobase/test'; import { SchemaNode } from '../dao/ui_schema_node_dao'; import UiSchemaRepository from '../repository'; @@ -366,6 +366,48 @@ describe('ui_schema repository', () => { }); }); + it('should get parent json schema', async () => { + await repository.insert({ + 'x-uid': 'n1', + name: 'a', + type: 'object', + properties: { + b: { + 'x-uid': 'n2', + type: 'object', + properties: { + c: { 'x-uid': 'n3' }, + }, + }, + d: { 'x-uid': 'n4' }, + }, + }); + + const parentSchema = await repository.getParentJsonSchema('n2'); + expect(parentSchema['x-uid']).toBe('n1'); + }); + + it('should get parent property', async () => { + await repository.insert({ + 'x-uid': 'n1', + name: 'a', + type: 'object', + properties: { + b: { + 'x-uid': 'n2', + type: 'object', + properties: { + c: { 'x-uid': 'n3' }, + }, + }, + d: { 'x-uid': 'n4' }, + }, + }); + + const parentProperty = await repository.getParentProperty('n2'); + expect(parentProperty['x-uid']).toBe('n1'); + }); + it('should getJsonSchema by subTree', async () => { await repository.insert({ 'x-uid': 'n1', diff --git a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/actions/ui-schema-action.ts b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/actions/ui-schema-action.ts index 308a5a7b8f..416fa0b717 100644 --- a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/actions/ui-schema-action.ts +++ b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/actions/ui-schema-action.ts @@ -50,6 +50,10 @@ export const uiSchemaActions = { readFromCache: true, }) as GetPropertiesOptions, ), + + getParentJsonSchema: callRepositoryMethod('getParentJsonSchema', 'resourceIndex'), + getParentProperty: callRepositoryMethod('getParentProperty', 'resourceIndex'), + insert: callRepositoryMethod('insert', 'values'), insertNewSchema: callRepositoryMethod('insertNewSchema', 'values'), remove: callRepositoryMethod('remove', 'resourceIndex'), diff --git a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/repository.ts b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/repository.ts index 7b7f5cf714..ea1a334a86 100644 --- a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/repository.ts +++ b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/repository.ts @@ -189,6 +189,26 @@ export class UiSchemaRepository extends Repository { return this.doGetProperties(uid, options); } + async getParentJsonSchema(uid: string, options: GetJsonSchemaOptions = {}) { + const parentUid = await this.findParentUid(uid, options.transaction); + + if (!parentUid) { + return null; + } + + return this.getJsonSchema(parentUid, options); + } + + async getParentProperty(uid: string, options: GetPropertiesOptions = {}) { + const parentUid = await this.findParentUid(uid, options.transaction); + + if (!parentUid) { + return null; + } + + return this.getJsonSchema(parentUid, options); + } + async getJsonSchema(uid: string, options?: GetJsonSchemaOptions): Promise { if (options?.readFromCache && this.cache) { return this.cache.wrap(`s_${uid}`, () => { diff --git a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/server.ts b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/server.ts index 64fa4f3723..4f04cb968b 100644 --- a/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/server.ts +++ b/packages/plugins/@nocobase/plugin-ui-schema-storage/src/server/server.ts @@ -33,20 +33,7 @@ export class UiSchemaStoragePlugin extends Plugin { this.app.acl.registerSnippet({ name: 'ui.uiSchemas', - actions: [ - 'uiSchemas:insert', - 'uiSchemas:insertNewSchema', - 'uiSchemas:remove', - 'uiSchemas:patch', - 'uiSchemas:batchPatch', - 'uiSchemas:clearAncestor', - 'uiSchemas:insertBeforeBegin', - 'uiSchemas:insertAfterBegin', - 'uiSchemas:insertBeforeEnd', - 'uiSchemas:insertAfterEnd', - 'uiSchemas:insertAdjacent', - 'uiSchemas:saveAsTemplate', - ], + actions: ['uiSchemas:*'], }); db.on('uiSchemas.beforeCreate', function setUid(model) {