fix: do not clear data when migrating data

This commit is contained in:
chenos 2020-12-16 08:42:37 +08:00
parent 1aa7ee65ab
commit 866549ce26
4 changed files with 227 additions and 141 deletions

View File

@ -5,10 +5,14 @@ import Database, { Model } from '@nocobase/database';
import actions from '../../../actions/src'; import actions from '../../../actions/src';
import associated from '../../../actions/src/middlewares/associated'; import associated from '../../../actions/src/middlewares/associated';
console.log(process.argv);
const clean = false;
const sync = { const sync = {
force: true, force: clean,
alter: { alter: {
drop: true, drop: clean,
}, },
}; };
@ -38,7 +42,8 @@ const api = Api.create({
api.resourcer.use(associated); api.resourcer.use(associated);
api.resourcer.registerActionHandlers({...actions.common, ...actions.associate}); api.resourcer.registerActionHandlers({...actions.common, ...actions.associate});
const data = { const data = [
{
title: '后台应用', title: '后台应用',
path: '/', path: '/',
type: 'layout', type: 'layout',
@ -133,7 +138,24 @@ const data = {
] ]
}, },
], ],
}; },
{
title: '登录页面',
path: '/login',
type: 'page',
inherit: false,
template: 'login',
order: 120,
},
{
title: '注册页面',
path: '/register',
type: 'page',
inherit: false,
template: 'register',
order: 130,
}
];
(async () => { (async () => {
await api await api
@ -150,44 +172,23 @@ const data = {
await database.sync({ await database.sync({
// tables: ['collections', 'fields', 'actions', 'views', 'tabs'], // tables: ['collections', 'fields', 'actions', 'views', 'tabs'],
}); });
const [Collection, Page, User] = database.getModels(['collections', 'pages', 'users']);
const Collection = database.getModel('collections');
const tables = database.getTables([]); const tables = database.getTables([]);
for (let table of tables) { for (let table of tables) {
await Collection.import(table.getOptions(), { migrate: false }); await Collection.import(table.getOptions(), { update: true, migrate: false });
} }
await Page.import(data);
const Page = database.getModel('pages'); await User.findOrCreate({
const page = await Page.create(data); where: {
await page.updateAssociations(data); username: "admin",
},
await Page.create({ defaults: {
title: '登录页面',
path: '/login',
type: 'page',
inherit: false,
template: 'login',
order: 120,
});
await Page.create({
title: '注册页面',
path: '/register',
type: 'page',
inherit: false,
template: 'register',
order: 130,
});
await database.getModel('users').create({
nickname: "超级管理员", nickname: "超级管理员",
password: "admin", password: "admin",
username: "admin", username: "admin",
token: "38979f07e1fca68fb3d2", token: "38979f07e1fca68fb3d2",
},
}); });
await database.getModel('collections').import(require('./collections/example').default); await database.getModel('collections').import(require('./collections/example').default);
await database.close(); await database.close();
})(); })();

View File

@ -129,21 +129,66 @@ export class CollectionModel extends BaseModel {
static async import(data: TableOptions, options: SaveOptions = {}): Promise<CollectionModel> { static async import(data: TableOptions, options: SaveOptions = {}): Promise<CollectionModel> {
data = _.cloneDeep(data); data = _.cloneDeep(data);
const collection = await this.create({ // @ts-ignore
...data, const { update } = options;
}, options); let collection;
const items: any = {}; if (data.name) {
collection = await this.findOne({
...options,
where: {
name: data.name,
},
});
} else if (data.title) {
collection = await this.findOne({
...options,
where: {
title: data.title,
},
});
}
if (collection && update) {
await collection.update(data, options);
}
if (!collection) {
collection = await this.create(data, options);
}
const associations = ['fields', 'tabs', 'actions', 'views']; const associations = ['fields', 'tabs', 'actions', 'views'];
for (const key of associations) { for (const key of associations) {
if (!Array.isArray(data[key])) { if (!Array.isArray(data[key])) {
continue; continue;
} }
items[key] = data[key].map((item, sort) => ({ const Model = this.database.getModel(key);
...item, for (const item of data[key]) {
sort, let model;
})); if (item.name) {
model = await Model.findOne({
...options,
where: {
collection_name: collection.name,
name: item.name,
},
});
} else if (item.title) {
model = await Model.findOne({
...options,
where: {
collection_name: collection.name,
title: item.title,
},
});
}
if (model && update) {
await model.update(item, options);
}
if (!model) {
model = await Model.create({
...item,
collection_name: collection.name,
}, options);
}
}
} }
await collection.updateAssociations(items, options);
return collection; return collection;
} }
} }

View File

@ -4,3 +4,4 @@ export * from './collection';
export * from './field'; export * from './field';
export * from './tab'; export * from './tab';
export * from './view'; export * from './view';
export * from './page';

View File

@ -0,0 +1,39 @@
import _ from 'lodash';
import BaseModel from './base';
import { SaveOptions, Op } from 'sequelize';
interface PageImportOptions extends SaveOptions {
parentId?: number;
}
/**
*
*/
export class PageModel extends BaseModel {
static async import(items: any, options: PageImportOptions = {}): Promise<any> {
const { parentId } = options;
if (!Array.isArray(items)) {
items = [items];
}
for (const item of items) {
let page = await this.findOne({
...options,
where: {
path: item.path,
},
});
if (!page) {
page = await this.create({
...item,
parent_id: parentId,
}, options);
}
if (Array.isArray(item.children)) {
await this.import(item.children, {
...options,
parentId: page.id,
});
}
}
}
}