feat(ui-schema-storage): support duplicate

This commit is contained in:
chenos 2023-10-16 16:04:50 +08:00
parent dc97149164
commit f603242e38
2 changed files with 43 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import { Collection, Database } from '@nocobase/database';
import { mockServer, MockServer } from '@nocobase/test';
import { MockServer, mockServer } from '@nocobase/test';
import { SchemaNode } from '../dao/ui_schema_node_dao';
import UiSchemaRepository from '../repository';
import PluginUiSchema from '../server';
@ -196,6 +196,34 @@ describe('ui_schema repository', () => {
});
});
it('should create a copy', async () => {
const s = 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',
properties: {
e: {
'x-uid': 'n5',
},
},
},
},
});
const s2 = await repository.duplicate(s['x-uid']);
expect(s2.name).toEqual(s.name);
expect(s2['x-uid']).not.toEqual(s['x-uid']);
});
describe('schema', () => {
let schema;
beforeEach(() => {

View File

@ -744,6 +744,20 @@ export class UiSchemaRepository extends Repository {
return insertedNodes;
}
private regenerateUid(s: any) {
s['x-uid'] = uid();
Object.keys(s.properties || {}).forEach((key) => {
this.regenerateUid(s.properties[key]);
});
}
@transaction()
async duplicate(uid: string, options?: Transactionable) {
const s = await this.getJsonSchema(uid, { ...options, includeAsyncNode: true });
this.regenerateUid(s);
return this.insert(s, options);
}
@transaction()
async insert(schema: any, options?: Transactionable) {
const nodes = UiSchemaRepository.schemaToSingleNodes(schema);