diff --git a/packages/core/client/src/collection-manager/interfaces/nanoid.ts b/packages/core/client/src/collection-manager/interfaces/nanoid.ts index adfd59fa2..09fddf21b 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 f54be0b34..b70b20668 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 bd6351c10..63fe69358 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 f9af33703..53705c6b8 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 d06890bdb..4b3952ec2 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 32fa2a3ab..dbcfd373d 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 000000000..d17e4c7db --- /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}$/); + }); +});