diff --git a/packages/plugin-collections/src/__tests__/models/base.test.ts b/packages/plugin-collections/src/__tests__/models/base.test.ts index e9870cdfc..56d8b9cbd 100644 --- a/packages/plugin-collections/src/__tests__/models/base.test.ts +++ b/packages/plugin-collections/src/__tests__/models/base.test.ts @@ -256,4 +256,31 @@ describe('models.base', () => { name: 'name1', }); }); + + it('component', async () => { + const t = await TestModel.create({ + component: { + arr: [ + {a: 'a', aa: 'aa'}, + {b: 'b', bb: 'bb'}, + {c: 'c', cc: 'cc'}, + ], + }, + }); + t.set({ + component: { + arr: [ + {a: 'aa'}, + {b: 'bb'}, + ], + } + }); + await t.save(); + expect(t.get('component')).toEqual({ + arr: [ + {a: 'aa'}, + {b: 'bb'}, + ], + }); + }) }); diff --git a/packages/plugin-collections/src/__tests__/models/field.test.ts b/packages/plugin-collections/src/__tests__/models/field.test.ts index 395f7858f..eb08b021b 100644 --- a/packages/plugin-collections/src/__tests__/models/field.test.ts +++ b/packages/plugin-collections/src/__tests__/models/field.test.ts @@ -1,6 +1,6 @@ import { Agent, getAgent, getApp } from '../'; import { Application } from '@nocobase/server'; -import * as types from '../../interfaces/types'; +import { options, types } from '../../interfaces'; jest.setTimeout(30000); import { FieldModel } from '../../models'; describe('models.field', () => { @@ -20,4 +20,34 @@ describe('models.field', () => { field.setInterface('updatedAt'); expect(field.get()).toMatchObject(types.updatedAt.options) }); + + it('dataSource', async () => { + const Collection = app.database.getModel('collections'); + // @ts-ignore + const collection = await Collection.create({ + title: 'tests', + }); + await collection.updateAssociations({ + fields: [ + { + title: 'xx', + name: 'xx', + interface: 'select', + type: 'virtual', + dataSource: [ + {label: 'xx', value: 'xx'}, + ], + component: { + type: 'string', + showInDetail: true, + showInForm: true, + }, + } + ], + }); + const fields = await collection.getFields(); + expect(fields[0].get('dataSource')).toEqual([ + {label: 'xx', value: 'xx'}, + ]); + }); }); diff --git a/packages/plugin-collections/src/hooks/fields-before-validate.ts b/packages/plugin-collections/src/hooks/fields-before-validate.ts index 46f4a3a98..d82109c3e 100644 --- a/packages/plugin-collections/src/hooks/fields-before-validate.ts +++ b/packages/plugin-collections/src/hooks/fields-before-validate.ts @@ -3,9 +3,9 @@ import * as types from '../interfaces/types'; import _ from 'lodash'; export default async function (model: FieldModel) { - if (model.get('interface')) { - model.setInterface(model.get('interface')); - } + // if (model.get('interface')) { + // model.setInterface(model.get('interface')); + // } // 生成随机 name 要放最后 model.generateNameIfNull(); } diff --git a/packages/plugin-collections/src/interfaces/types.ts b/packages/plugin-collections/src/interfaces/types.ts index 4217f5296..c34d83304 100644 --- a/packages/plugin-collections/src/interfaces/types.ts +++ b/packages/plugin-collections/src/interfaces/types.ts @@ -208,7 +208,8 @@ export const datetime = { showTime: false, filterable: true, sortable: true, - format: 'YYYY-MM-DD HH:mm:ss', + dateFormat: 'YYYY-MM-DD', + timeFormat: 'HH:mm:ss', component: { type: 'date', }, @@ -222,7 +223,7 @@ export const time = { type: 'time', filterable: true, sortable: true, - format: 'HH:mm:ss', + timeFormat: 'HH:mm:ss', component: { type: 'time', }, @@ -271,7 +272,7 @@ export const subTable = { type: 'hasMany', // fields: [], component: { - type: 'subTable', + type: 'table', }, }, }; @@ -351,6 +352,8 @@ export const createdAt = { type: 'date', name: 'created_at', showTime: true, + dateFormat: 'YYYY-MM-DD', + timeFormat: 'HH:mm:ss', required: true, filterable: true, sortable: true, @@ -373,12 +376,14 @@ export const updatedBy = { }; export const updatedAt = { - title: '更新日期', + title: '更新时间', options: { interface: 'updatedAt', type: 'date', name: 'updated_at', showTime: true, + dateFormat: 'YYYY-MM-DD', + timeFormat: 'HH:mm:ss', required: true, filterable: true, sortable: true, diff --git a/packages/plugin-collections/src/models/base.ts b/packages/plugin-collections/src/models/base.ts index d861d8874..4a9effa83 100644 --- a/packages/plugin-collections/src/models/base.ts +++ b/packages/plugin-collections/src/models/base.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; import { getDataTypeKey, Model } from '@nocobase/database'; -import { Utils } from 'sequelize'; +import { merge } from '../utils'; export function generateName(title?: string): string { return `${Math.random().toString(36).replace('0.', '').slice(-4).padStart(4, '0')}`; @@ -83,7 +83,7 @@ export class BaseModel extends Model { // 如果是 object 数据,merge 处理 if (_.isPlainObject(value)) { // @ts-ignore - value = Utils.merge(this.get(key)||{}, value); + value = merge(this.get(key)||{}, value); } const [column, ...path] = key.split('.'); if (!options.raw) { diff --git a/packages/plugin-collections/src/models/field.ts b/packages/plugin-collections/src/models/field.ts index 600feb1d9..b3b9049a6 100644 --- a/packages/plugin-collections/src/models/field.ts +++ b/packages/plugin-collections/src/models/field.ts @@ -2,7 +2,8 @@ import _ from 'lodash'; import BaseModel from './base'; import { FieldOptions } from '@nocobase/database'; import * as types from '../interfaces/types'; -import { Utils } from 'sequelize'; +import { merge } from '../utils'; +import { BuildOptions } from 'sequelize'; export function generateFieldName(title?: string): string { return `f_${Math.random().toString(36).replace('0.', '').slice(-4).padStart(4, '0')}`; @@ -10,6 +11,22 @@ export function generateFieldName(title?: string): string { export class FieldModel extends BaseModel { + constructor(values: any = {}, options: any = {}) { + let data = { + ...(values.options||{}), + ..._.omit(values, 'options'), + }; + const interfaceType = data.interface; + if (interfaceType) { + const { options } = types[interfaceType]; + let args = [options, data]; + // @ts-ignore + data = merge(...args); + } + // @ts-ignore + super(data, options); + } + generateName() { this.set('name', generateFieldName()); } @@ -31,7 +48,7 @@ export class FieldModel extends BaseModel { args = [options, this.get()]; } // @ts-ignore - const values = Utils.merge(...args); + const values = merge(...args); this.set(values); } diff --git a/packages/plugin-collections/src/utils.ts b/packages/plugin-collections/src/utils.ts new file mode 100644 index 000000000..e58cbe491 --- /dev/null +++ b/packages/plugin-collections/src/utils.ts @@ -0,0 +1,9 @@ +import deepmerge from 'deepmerge'; + +const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray + +export function merge(obj1: any, obj2: any) { + return deepmerge(obj1, obj2, { + arrayMerge: overwriteMerge, + }); +}