From b878b214d413e9e7e643eca83763d0136d58ede5 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 7 Jan 2021 14:54:01 +0800 Subject: [PATCH] add collectionSync parameter for database options --- .../src/hooks/collections-after-create.ts | 3 ++- .../plugin-collections/src/models/collection.ts | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/plugin-collections/src/hooks/collections-after-create.ts b/packages/plugin-collections/src/hooks/collections-after-create.ts index 3ca3481747..ebc468b23f 100644 --- a/packages/plugin-collections/src/hooks/collections-after-create.ts +++ b/packages/plugin-collections/src/hooks/collections-after-create.ts @@ -77,8 +77,9 @@ const defaultValues = { export default async function (model: CollectionModel, options: any = {}) { const { migrate = true } = options; + console.log({migrate}) if (migrate) { - await model.migrate(options); + await model.migrate({...options, isNewRecord: true}); } await model.updateAssociations(defaultValues, options); } diff --git a/packages/plugin-collections/src/models/collection.ts b/packages/plugin-collections/src/models/collection.ts index 046ff5b1ee..31259bb2e4 100644 --- a/packages/plugin-collections/src/models/collection.ts +++ b/packages/plugin-collections/src/models/collection.ts @@ -86,8 +86,21 @@ export class CollectionModel extends BaseModel { * 迁移 */ async migrate(options: MigrateOptions = {}) { + const { isNewRecord } = options; const table = await this.loadTableOptions(options); - return await table.sync({ + // 如果不是新增数据,force 必须为 false + if (!isNewRecord) { + return await table.sync({ + force: false, + alter: { + drop: false, + } + }); + } + // TODO: 暂时加了个 collectionSync 解决 collection.create 的数据不清空问题 + // @ts-ignore + const sync = this.sequelize.options.collectionSync; + return await table.sync(sync || { force: false, alter: { drop: false,