From f603242e3808909ca5277a13c76b7cd77adaf98d Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 16 Oct 2023 16:04:50 +0800 Subject: [PATCH] feat(ui-schema-storage): support duplicate --- .../__tests__/ui-schema-repository.test.ts | 30 ++++++++++++++++++- .../src/server/repository.ts | 14 +++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) 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 7004fea86..b63e62a09 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 { 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(() => { 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 274c3b4bc..a81a9207d 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 @@ -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);