feat: get json schema with async node (#246)

This commit is contained in:
ChengLei Shao 2022-03-21 14:51:23 +08:00 committed by GitHub
parent ed8c60eb85
commit 5554b1c260
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 4 deletions

View File

@ -56,6 +56,43 @@ describe('action test', () => {
expect(response.statusCode).toEqual(200);
});
test('getJsonSchema with async node', async () => {
await app
.agent()
.resource('uiSchemas')
.insert({
values: {
'x-uid': 'n1',
name: 'a',
type: 'object',
properties: {
b: {
'x-async': true,
'x-uid': 'n2',
type: 'object',
properties: {
c: { 'x-uid': 'n3' },
},
},
d: { 'x-uid': 'n4' },
},
},
});
let response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'n1',
});
expect(response.body.data.properties.b).toBeUndefined();
response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'n1',
includeAsyncNode: true,
});
expect(response.body.data.properties.b).toBeDefined();
});
test('getJsonSchema', async () => {
await app
.agent()

View File

@ -1,17 +1,19 @@
import { Context } from '@nocobase/actions';
import lodash from 'lodash';
import UiSchemaRepository from '../repository';
import { ActionParams } from '@nocobase/resourcer';
const getRepositoryFromCtx = (ctx: Context) => {
return ctx.db.getCollection('uiSchemas').repository as UiSchemaRepository;
};
const callRepositoryMethod = (method, paramsKey: 'resourceIndex' | 'values') => {
const callRepositoryMethod = (method, paramsKey: 'resourceIndex' | 'values', optionsBuilder?) => {
return async (ctx, next) => {
const params = lodash.get(ctx.action.params, paramsKey);
const options = optionsBuilder ? optionsBuilder(ctx.action.params) : {};
const repository = getRepositoryFromCtx(ctx);
const returnValue = await repository[method](params);
const returnValue = await repository[method](params, options);
ctx.body = returnValue || {
result: 'ok',
@ -30,7 +32,12 @@ function parseInsertAdjacentValues(values) {
}
export const uiSchemaActions = {
getJsonSchema: callRepositoryMethod('getJsonSchema', 'resourceIndex'),
getJsonSchema: callRepositoryMethod('getJsonSchema', 'resourceIndex', (params: ActionParams) => {
return {
includeAsyncNode: params?.includeAsyncNode,
};
}),
getProperties: callRepositoryMethod('getProperties', 'resourceIndex'),
insert: callRepositoryMethod('insert', 'values'),
insertNewSchema: callRepositoryMethod('insertNewSchema', 'values'),

View File

@ -144,7 +144,9 @@ export class UiSchemaRepository extends Repository {
return carry;
}
async getProperties(uid: string) {
async getProperties(uid: string, options: TransactionAble = {}) {
const { transaction } = options;
const db = this.database;
const rawSql = `
@ -161,6 +163,7 @@ export class UiSchemaRepository extends Repository {
replacements: {
ancestor: uid,
},
transaction,
});
if (nodes[0].length == 0) {