From 808e3657e0d982f8e9b3332f9d2b787adb29409e Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 18 Mar 2021 22:33:57 +0800 Subject: [PATCH] improve collection/field import --- .../src/models/collection.ts | 46 +++---------------- .../plugin-collections/src/models/field.ts | 9 ---- 2 files changed, 6 insertions(+), 49 deletions(-) diff --git a/packages/plugin-collections/src/models/collection.ts b/packages/plugin-collections/src/models/collection.ts index 2818a56d8..e611b1a6f 100644 --- a/packages/plugin-collections/src/models/collection.ts +++ b/packages/plugin-collections/src/models/collection.ts @@ -58,7 +58,7 @@ export class CollectionModel extends BaseModel { // const prevTable = this.database.getTable(this.get('name')); // const prevOptions = prevTable ? prevTable.getOptions() : {}; // table 是初始化和重新初始化 - const table = this.database.extend(options); + const table = this.database.table(options); // console.log({options, actions: table.getOptions()['actions']}) // 如果关系表未加载,一起处理 @@ -124,6 +124,7 @@ export class CollectionModel extends BaseModel { ...this.get(), actions: await this.getActions(), fields: await this.getFieldsOptions(), + views_v2: await this.getViews_v2(), }; } @@ -161,15 +162,8 @@ export class CollectionModel extends BaseModel { name: data.name, }, }); - } else if (data.title) { - collection = await this.findOne({ - ...options, - where: { - title: data.title, - }, - }); } - if (collection && update) { + if (collection) { // @ts-ignore await collection.update(data, options); } @@ -178,19 +172,16 @@ export class CollectionModel extends BaseModel { collection = await this.create(data, options); } - await collection.updateAssociations(data, options); - return; - const associations = ['fields', 'tabs', 'actions', 'views', 'pages_v2', 'views_v2']; + const associations = ['fields', 'actions', 'views_v2']; for (const key of associations) { if (!Array.isArray(data[key])) { continue; } const Model = this.database.getModel(key); let ids = []; - const View = this.database.getModel('views'); for (const index in data[key]) { if (key === 'fields') { - ids = await Field.import(data[key], { + ids = await Model.import(data[key], { ...options, collectionName: collection.name, }); @@ -206,16 +197,8 @@ export class CollectionModel extends BaseModel { name: item.name, }, }); - } else if (item.title) { - model = await Model.findOne({ - ...options, - where: { - collection_name: collection.name, - title: item.title, - }, - }); } - if (model && update) { + if (model) { await model.update({...item, // sort: index+1 }, options); @@ -232,23 +215,6 @@ export class CollectionModel extends BaseModel { ); } if (model) { - if (key === 'tabs') { - let associationField; - if (item.association) { - associationField = await Field.findOne({ - where: { - name: item.association, - collection_name: collection.name, - }, - }); - // TODO: tabs 表还未创建,暂时先这么处理 - if (associationField) { - await model.updateAssociations({ - associationField: associationField.id, - }); - } - } - } ids.push(model.id); } } diff --git a/packages/plugin-collections/src/models/field.ts b/packages/plugin-collections/src/models/field.ts index 7b071cddc..cc0cfdf8d 100644 --- a/packages/plugin-collections/src/models/field.ts +++ b/packages/plugin-collections/src/models/field.ts @@ -226,15 +226,6 @@ export class FieldModel extends BaseModel { }, }); } - if (!model && item.title) { - model = await this.findOne({ - ...options, - where: { - ...where, - title: item.title, - }, - }); - } if (!model) { const tmp: any = {}; if (parentId) {