From 5153ce9ab296da9ff75fec927d5e75fad154d285 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 20 Mar 2024 09:39:11 +0800 Subject: [PATCH] fix: uuid field (#3736) * fix: uuid test * chore: uuid test * fix: test * fix: uuid field name support edit --------- Co-authored-by: katherinehhh --- .../collection-manager/interfaces/nanoid.ts | 1 - .../src/collection-manager/interfaces/uuid.ts | 1 - .../src/__tests__/fields/uuid-field.test.ts | 11 +++-- .../core/database/src/fields/uuid-field.ts | 2 +- .../src/server/__tests__/dumper.test.ts | 37 +++++++++++++++ .../src/server/dumper.ts | 8 +++- .../src/server/__tests__/fields/uuid.test.ts | 46 +++++++++++++++++++ 7 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/uuid.test.ts diff --git a/packages/core/client/src/collection-manager/interfaces/nanoid.ts b/packages/core/client/src/collection-manager/interfaces/nanoid.ts index adfd59fa2c..09fddf21b9 100644 --- a/packages/core/client/src/collection-manager/interfaces/nanoid.ts +++ b/packages/core/client/src/collection-manager/interfaces/nanoid.ts @@ -28,7 +28,6 @@ export class NanoidFieldInterface extends CollectionFieldInterface { type: 'string', title: '{{t("Field name")}}', required: true, - 'x-disabled': true, 'x-decorator': 'FormItem', 'x-component': 'Input', description: diff --git a/packages/core/client/src/collection-manager/interfaces/uuid.ts b/packages/core/client/src/collection-manager/interfaces/uuid.ts index f54be0b344..b70b20668b 100644 --- a/packages/core/client/src/collection-manager/interfaces/uuid.ts +++ b/packages/core/client/src/collection-manager/interfaces/uuid.ts @@ -30,7 +30,6 @@ export class UUIDFieldInterface extends CollectionFieldInterface { type: 'string', title: '{{t("Field name")}}', required: true, - 'x-disabled': true, 'x-decorator': 'FormItem', 'x-component': 'Input', description: diff --git a/packages/core/database/src/__tests__/fields/uuid-field.test.ts b/packages/core/database/src/__tests__/fields/uuid-field.test.ts index bd6351c10b..63fe69358d 100644 --- a/packages/core/database/src/__tests__/fields/uuid-field.test.ts +++ b/packages/core/database/src/__tests__/fields/uuid-field.test.ts @@ -13,19 +13,20 @@ describe('string field', () => { await db.close(); }); - it('define', async () => { + it('should create uuid field', async () => { const Test = db.collection({ name: 'tests', - autoGenId: false, fields: [ { + name: 'uuid', type: 'uuid', - name: 'id', - primaryKey: true, }, ], }); + await Test.sync(); - await Test.model.create(); + const item = await Test.model.create(); + + expect(item['uuid']).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); }); }); diff --git a/packages/core/database/src/fields/uuid-field.ts b/packages/core/database/src/fields/uuid-field.ts index f9af33703e..53705c6b8e 100644 --- a/packages/core/database/src/fields/uuid-field.ts +++ b/packages/core/database/src/fields/uuid-field.ts @@ -5,7 +5,7 @@ export class UuidField extends Field { constructor(options?: any, context?: FieldContext) { super( { - defaultValue: DataTypes.UUIDV4, + defaultValue: new DataTypes.UUIDV4(), ...options, }, context, diff --git a/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts b/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts index d06890bdb7..4b3952ec2f 100644 --- a/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts +++ b/packages/plugins/@nocobase/plugin-backup-restore/src/server/__tests__/dumper.test.ts @@ -85,6 +85,43 @@ describe('dumper', () => { expect(items.length).toBe(1); }); + it('should dump and restore uuid field', async () => { + await db.getRepository('collections').create({ + values: { + name: 'tests', + fields: [ + { + type: 'uuid', + name: 'uuid_test', + }, + ], + }, + context: {}, + }); + + await db.getRepository('tests').create({ + values: {}, + }); + + const dumper = new Dumper(app); + + const result = await dumper.dump({ + groups: new Set(['required', 'custom']), + }); + + const restorer = new Restorer(app, { + backUpFilePath: result.filePath, + }); + + await restorer.restore({ + groups: new Set(['required', 'custom']), + }); + + const testCollection = app.db.getCollection('tests'); + const items = await testCollection.repository.find(); + expect(items.length).toBe(1); + }); + describe('id seq', () => { let allGroups; diff --git a/packages/plugins/@nocobase/plugin-backup-restore/src/server/dumper.ts b/packages/plugins/@nocobase/plugin-backup-restore/src/server/dumper.ts index 32fa2a3ab2..dbcfd373d2 100644 --- a/packages/plugins/@nocobase/plugin-backup-restore/src/server/dumper.ts +++ b/packages/plugins/@nocobase/plugin-backup-restore/src/server/dumper.ts @@ -412,12 +412,18 @@ export class Dumper extends AppMigrator { if (collectionField) { // is a field - return { + const fieldAttributes: any = { field: attr.field, isCollectionField: true, type: collectionField.type, typeOptions: collectionField.options, }; + + if (fieldAttributes.typeOptions?.defaultValue?.constructor?.name === 'UUIDV4') { + delete fieldAttributes.typeOptions.defaultValue; + } + + return fieldAttributes; } return { diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/uuid.test.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/uuid.test.ts new file mode 100644 index 0000000000..d17e4c7dbf --- /dev/null +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/uuid.test.ts @@ -0,0 +1,46 @@ +import Database, { Collection as DBCollection } from '@nocobase/database'; +import Application from '@nocobase/server'; +import { createApp } from '..'; + +describe('uuid', () => { + let db: Database; + let app: Application; + let Collection: DBCollection; + let Field: DBCollection; + + beforeEach(async () => { + app = await createApp(); + db = app.db; + Collection = db.getCollection('collections'); + Field = db.getCollection('fields'); + }); + + afterEach(async () => { + await app.destroy(); + }); + + it('should create uuid field', async () => { + await Collection.repository.create({ + values: { + name: 'tests', + }, + context: {}, + }); + + await Field.repository.create({ + values: { + collectionName: 'tests', + type: 'uuid', + name: 'uuid', + }, + context: {}, + }); + + // @ts-ignore + const resp = await app.agent().resource('tests').create({}); + expect(resp.status).toBe(200); + + const data = resp.body.data; + expect(data['uuid']).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); + }); +});