fix: uuid field (#3736)

* fix: uuid test

* chore: uuid test

* fix: test

* fix: uuid field name support edit

---------

Co-authored-by: katherinehhh <katherine_15995@163.com>
This commit is contained in:
ChengLei Shao 2024-03-20 09:39:11 +08:00 committed by GitHub
parent 54f6597b9d
commit 5153ce9ab2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 97 additions and 9 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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}$/);
});
});

View File

@ -5,7 +5,7 @@ export class UuidField extends Field {
constructor(options?: any, context?: FieldContext) {
super(
{
defaultValue: DataTypes.UUIDV4,
defaultValue: new DataTypes.UUIDV4(),
...options,
},
context,

View File

@ -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;

View File

@ -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 {

View File

@ -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}$/);
});
});