From a44bab62fca857f8ab1df3b3ce3584e51be79d36 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 8 Dec 2020 14:35:50 +0800 Subject: [PATCH] fix: add transaction to migrate options --- .../src/hooks/collections-after-create.ts | 9 +++++++-- .../src/hooks/fields-after-bulk-update.ts | 2 +- .../plugin-collections/src/hooks/fields-after-create.ts | 2 +- packages/plugin-collections/src/interfaces/types.ts | 1 + packages/plugin-collections/src/models/collection.ts | 7 +++++-- packages/plugin-collections/src/models/field.ts | 7 ++++--- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/plugin-collections/src/hooks/collections-after-create.ts b/packages/plugin-collections/src/hooks/collections-after-create.ts index d846d44adb..08aad2534f 100644 --- a/packages/plugin-collections/src/hooks/collections-after-create.ts +++ b/packages/plugin-collections/src/hooks/collections-after-create.ts @@ -2,6 +2,11 @@ import CollectionModel from '../models/collection'; const defaultValues = { actions: [ + { + type: 'filter', + name: 'filter', + title: '筛选', + }, { type: 'list', name: 'list', @@ -55,7 +60,7 @@ const defaultValues = { name: 'table', title: '列表', template: 'Table', - actionNames: ['create', 'destroy'], + actionNames: ['filter', 'create', 'destroy'], default: true, }, ], @@ -73,7 +78,7 @@ const defaultValues = { export default async function (model: CollectionModel, options: any = {}) { const { migrate = true } = options; if (migrate) { - await model.migrate(); + await model.migrate(options); } await model.updateAssociations(defaultValues, options); } diff --git a/packages/plugin-collections/src/hooks/fields-after-bulk-update.ts b/packages/plugin-collections/src/hooks/fields-after-bulk-update.ts index 1af384d6d1..6c897ac95c 100644 --- a/packages/plugin-collections/src/hooks/fields-after-bulk-update.ts +++ b/packages/plugin-collections/src/hooks/fields-after-bulk-update.ts @@ -14,7 +14,7 @@ export default async function (options: any = {}) { transaction, }); for (const field of fields) { - await field.migrate(); + await field.migrate(options); } } } diff --git a/packages/plugin-collections/src/hooks/fields-after-create.ts b/packages/plugin-collections/src/hooks/fields-after-create.ts index 5ea8f78c96..9c4dfb44fb 100644 --- a/packages/plugin-collections/src/hooks/fields-after-create.ts +++ b/packages/plugin-collections/src/hooks/fields-after-create.ts @@ -3,6 +3,6 @@ import FieldModel from '../models/field'; export default async function (model: FieldModel, options: any = {}) { const { migrate = true } = options; if (migrate) { - await model.migrate(); + await model.migrate(options); } } diff --git a/packages/plugin-collections/src/interfaces/types.ts b/packages/plugin-collections/src/interfaces/types.ts index ae2808a834..7e5f17fafb 100644 --- a/packages/plugin-collections/src/interfaces/types.ts +++ b/packages/plugin-collections/src/interfaces/types.ts @@ -10,6 +10,7 @@ export const string = { options: { interface: 'string', type: 'string', + filterable: true, component: { type: 'string', }, diff --git a/packages/plugin-collections/src/models/collection.ts b/packages/plugin-collections/src/models/collection.ts index a298d5cdcc..ec73d2d9da 100644 --- a/packages/plugin-collections/src/models/collection.ts +++ b/packages/plugin-collections/src/models/collection.ts @@ -74,6 +74,7 @@ export class CollectionModel extends BaseModel { } if (associationTableNames.length) { await CollectionModel.load({ + ...opts, where: { name: { [Op.in]: associationTableNames, @@ -88,7 +89,7 @@ export class CollectionModel extends BaseModel { * 迁移 */ async migrate(options: MigrateOptions = {}) { - const table = await this.loadTableOptions(); + const table = await this.loadTableOptions(options); return await table.sync({ force: false, alter: { @@ -121,12 +122,14 @@ export class CollectionModel extends BaseModel { * @param options */ static async load(options: LoadOptions = {}) { - const { reset = false, where = {} } = options; + const { reset = false, where = {}, transaction } = options; const collections = await this.findAll({ + transaction, where, }); for (const collection of collections) { await collection.loadTableOptions({ + transaction, reset, }); } diff --git a/packages/plugin-collections/src/models/field.ts b/packages/plugin-collections/src/models/field.ts index 3e8270146e..b86f213e07 100644 --- a/packages/plugin-collections/src/models/field.ts +++ b/packages/plugin-collections/src/models/field.ts @@ -35,11 +35,12 @@ export class FieldModel extends BaseModel { }; } - async migrate() { - if (!this.get('collection_name')) { + async migrate(options: any = {}) { + const collectionName = this.get('collection_name'); + if (!collectionName) { return false; } - const table = this.database.getTable(this.get('collection_name')); + const table = this.database.getTable(collectionName); table.addField(await this.getOptions()); await table.sync({ force: false,