mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 07:45:18 +00:00
fix: field migrate bug when import collection options
This commit is contained in:
parent
ed43bfe86d
commit
524103f6b3
@ -1,9 +1,9 @@
|
||||
import Database, { ModelCtor } from '@nocobase/database';
|
||||
import { getDatabase } from '.';
|
||||
import BaseModel from '../models/base';
|
||||
import { getDatabase } from '../';
|
||||
import BaseModel from '../../models/base';
|
||||
import _ from 'lodash';
|
||||
|
||||
describe('base model', () => {
|
||||
describe('models.base', () => {
|
||||
let database: Database;
|
||||
let TestModel: ModelCtor<BaseModel>;
|
||||
let test: BaseModel;
|
@ -0,0 +1,60 @@
|
||||
import { Agent, getAgent, getApp } from '../';
|
||||
import { Application } from '@nocobase/server';
|
||||
import * as types from '../../interfaces/types';
|
||||
jest.setTimeout(30000);
|
||||
describe('models.collection', () => {
|
||||
let app: Application;
|
||||
let agent: Agent;
|
||||
|
||||
beforeEach(async () => {
|
||||
app = await getApp();
|
||||
agent = getAgent(app);
|
||||
});
|
||||
|
||||
afterEach(() => app.database.close());
|
||||
|
||||
it.only('import', async () => {
|
||||
await app.database.getModel('collections').import({
|
||||
title: '示例',
|
||||
name: 'examples',
|
||||
showInDataMenu: true,
|
||||
fields: [
|
||||
{
|
||||
interface: 'string',
|
||||
title: '单行文本',
|
||||
name: 'string',
|
||||
component: {
|
||||
showInTable: true,
|
||||
showInDetail: true,
|
||||
showInForm: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
interface: 'textarea',
|
||||
title: '多行文本',
|
||||
name: 'textarea',
|
||||
component: {
|
||||
showInTable: true,
|
||||
showInDetail: true,
|
||||
showInForm: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
}, {
|
||||
// migrate: false,
|
||||
});
|
||||
const table = app.database.getTable('examples');
|
||||
expect(table).toBeDefined();
|
||||
expect(table.getFields().size).toBe(2);
|
||||
await table.sync();
|
||||
const Example = app.database.getModel('examples');
|
||||
const example = await Example.create({
|
||||
string: 'string1',
|
||||
textarea: 'textarea1',
|
||||
});
|
||||
expect(example.toJSON()).toMatchObject({
|
||||
string: 'string1',
|
||||
textarea: 'textarea1',
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,20 @@
|
||||
import { Model, ModelCtor } from '@nocobase/database';
|
||||
|
||||
/**
|
||||
* 字段导入是先 create 再 bulk update 处理 fk 的
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
export default async function (options: any = {}) {
|
||||
const { migrate = true, where = {}, attributes: { collection_name }, transaction } = options;
|
||||
if (migrate && collection_name) {
|
||||
const Field = this.database.getModel('fields') as ModelCtor<Model>;
|
||||
const fields = await Field.findAll({
|
||||
where,
|
||||
transaction,
|
||||
});
|
||||
for (const field of fields) {
|
||||
await field.migrate();
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import collectionsAfterCreate from './collections-after-create';
|
||||
import fieldsBeforeValidate from './fields-before-validate';
|
||||
import fieldsAfterCreate from './fields-after-create';
|
||||
import generateName from './generateName';
|
||||
import fieldsAfterBulkUpdate from './fields-after-bulk-update';
|
||||
|
||||
export default {
|
||||
collections: {
|
||||
@ -12,7 +13,8 @@ export default {
|
||||
},
|
||||
fields: {
|
||||
beforeValidate: fieldsBeforeValidate,
|
||||
afterCreate: fieldsAfterCreate
|
||||
afterCreate: fieldsAfterCreate,
|
||||
afterBulkUpdate: fieldsAfterBulkUpdate,
|
||||
},
|
||||
actions: {
|
||||
beforeValidate: generateName
|
||||
|
@ -36,6 +36,9 @@ export class FieldModel extends BaseModel {
|
||||
}
|
||||
|
||||
async migrate() {
|
||||
if (!this.get('collection_name')) {
|
||||
return false;
|
||||
}
|
||||
const table = this.database.getTable(this.get('collection_name'));
|
||||
table.addField(await this.getOptions());
|
||||
await table.sync({
|
||||
|
@ -26,7 +26,7 @@ export default async function (this: Application, options = {}) {
|
||||
// 加载数据库表 collections 中已经保存的表配置
|
||||
// 如果未 sync 可能报错
|
||||
// TODO collections sync
|
||||
await database.getModel('collections').load();
|
||||
// await database.getModel('collections').load();
|
||||
} catch (error) {
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user