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 <chenlinxh@gmail.com>
This commit is contained in:
ChengLei Shao 2024-04-07 13:14:48 +08:00 committed by GitHub
parent ac1e07ff52
commit dfcf7671d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 98 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import { Database } from '@nocobase/database'; import { Database } from '@nocobase/database';
import { MockServer, createMockServer } from '@nocobase/test'; import { createMockServer, MockServer } from '@nocobase/test';
describe('action test', () => { describe('action test', () => {
let app: MockServer; let app: MockServer;
@ -17,6 +17,35 @@ describe('action test', () => {
await app.destroy(); 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 () => { test('insert action', async () => {
const response = await app const response = await app
.agent() .agent()

View File

@ -1,5 +1,5 @@
import { Collection, Database } from '@nocobase/database'; 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 { SchemaNode } from '../dao/ui_schema_node_dao';
import UiSchemaRepository from '../repository'; 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 () => { it('should getJsonSchema by subTree', async () => {
await repository.insert({ await repository.insert({
'x-uid': 'n1', 'x-uid': 'n1',

View File

@ -50,6 +50,10 @@ export const uiSchemaActions = {
readFromCache: true, readFromCache: true,
}) as GetPropertiesOptions, }) as GetPropertiesOptions,
), ),
getParentJsonSchema: callRepositoryMethod('getParentJsonSchema', 'resourceIndex'),
getParentProperty: callRepositoryMethod('getParentProperty', 'resourceIndex'),
insert: callRepositoryMethod('insert', 'values'), insert: callRepositoryMethod('insert', 'values'),
insertNewSchema: callRepositoryMethod('insertNewSchema', 'values'), insertNewSchema: callRepositoryMethod('insertNewSchema', 'values'),
remove: callRepositoryMethod('remove', 'resourceIndex'), remove: callRepositoryMethod('remove', 'resourceIndex'),

View File

@ -189,6 +189,26 @@ export class UiSchemaRepository extends Repository {
return this.doGetProperties(uid, options); 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<any> { async getJsonSchema(uid: string, options?: GetJsonSchemaOptions): Promise<any> {
if (options?.readFromCache && this.cache) { if (options?.readFromCache && this.cache) {
return this.cache.wrap(`s_${uid}`, () => { return this.cache.wrap(`s_${uid}`, () => {

View File

@ -33,20 +33,7 @@ export class UiSchemaStoragePlugin extends Plugin {
this.app.acl.registerSnippet({ this.app.acl.registerSnippet({
name: 'ui.uiSchemas', name: 'ui.uiSchemas',
actions: [ actions: ['uiSchemas:*'],
'uiSchemas:insert',
'uiSchemas:insertNewSchema',
'uiSchemas:remove',
'uiSchemas:patch',
'uiSchemas:batchPatch',
'uiSchemas:clearAncestor',
'uiSchemas:insertBeforeBegin',
'uiSchemas:insertAfterBegin',
'uiSchemas:insertBeforeEnd',
'uiSchemas:insertAfterEnd',
'uiSchemas:insertAdjacent',
'uiSchemas:saveAsTemplate',
],
}); });
db.on('uiSchemas.beforeCreate', function setUid(model) { db.on('uiSchemas.beforeCreate', function setUid(model) {