From c5e0f65ff576c4082dc443113ec2d37e2e98179b Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 21 Dec 2020 10:42:44 +0800 Subject: [PATCH] feat: add field import function --- .../src/models/collection.ts | 10 ++- .../plugin-collections/src/models/field.ts | 64 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/packages/plugin-collections/src/models/collection.ts b/packages/plugin-collections/src/models/collection.ts index 0c7f07e3b..c652fa289 100644 --- a/packages/plugin-collections/src/models/collection.ts +++ b/packages/plugin-collections/src/models/collection.ts @@ -1,5 +1,6 @@ import _ from 'lodash'; import BaseModel from './base'; +import Field from './field'; import { TableOptions } from '@nocobase/database'; import { SaveOptions, Op } from 'sequelize'; @@ -159,8 +160,15 @@ export class CollectionModel extends BaseModel { continue; } const Model = this.database.getModel(key); - const ids = []; + let ids = []; for (const index in data[key]) { + if (key === 'fields') { + ids = await Field.import(data[key], { + ...options, + collectionName: collection.name, + }); + continue; + } let model; const item = data[key][index]; if (item.name) { diff --git a/packages/plugin-collections/src/models/field.ts b/packages/plugin-collections/src/models/field.ts index 30ef18917..a1fab8993 100644 --- a/packages/plugin-collections/src/models/field.ts +++ b/packages/plugin-collections/src/models/field.ts @@ -7,6 +7,11 @@ import { BuildOptions } from 'sequelize'; import { SaveOptions, Utils } from 'sequelize'; import { generateCollectionName } from './collection'; +interface FieldImportOptions extends SaveOptions { + parentId?: number; + collectionName?: string; +} + export function generateFieldName(title?: string): string { return `f_${Math.random().toString(36).replace('0.', '').slice(-4).padStart(4, '0')}`; } @@ -85,6 +90,65 @@ export class FieldModel extends BaseModel { } }); } + + static async import(items: any, options: FieldImportOptions = {}): Promise { + const { parentId, collectionName } = options; + if (!Array.isArray(items)) { + items = [items]; + } + const ids = []; + for (const index in items) { + const item = items[index]; + let model; + const where: any = {}; + if (parentId) { + where.parent_id = parentId + } else { + where.collection_name = collectionName; + } + if (item.name) { + model = await this.findOne({ + ...options, + where: { + ...where, + name: item.name, + }, + }); + } + if (!model && item.title) { + model = await this.findOne({ + ...options, + where: { + ...where, + title: item.title, + }, + }); + } + if (!model) { + const tmp: any = {}; + if (parentId) { + tmp.parent_id = parentId + } else { + tmp.collection_name = collectionName; + } + model = await this.create({ + ...item, + ...tmp, + }, options); + } + if (Array.isArray(item.children)) { + const childrenIds = await this.import(item.children, { + ...options, + parentId: model.id, + collectionName, + }); + await model.updateAssociations({ + children: childrenIds, + }, options); + } + } + return ids; + } } export default FieldModel;