add collectionSync parameter for database options

This commit is contained in:
chenos 2021-01-07 14:54:01 +08:00
parent 8289f96dd5
commit b878b214d4
2 changed files with 16 additions and 2 deletions

View File

@ -77,8 +77,9 @@ const defaultValues = {
export default async function (model: CollectionModel, options: any = {}) { export default async function (model: CollectionModel, options: any = {}) {
const { migrate = true } = options; const { migrate = true } = options;
console.log({migrate})
if (migrate) { if (migrate) {
await model.migrate(options); await model.migrate({...options, isNewRecord: true});
} }
await model.updateAssociations(defaultValues, options); await model.updateAssociations(defaultValues, options);
} }

View File

@ -86,8 +86,21 @@ export class CollectionModel extends BaseModel {
* *
*/ */
async migrate(options: MigrateOptions = {}) { async migrate(options: MigrateOptions = {}) {
const { isNewRecord } = options;
const table = await this.loadTableOptions(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, force: false,
alter: { alter: {
drop: false, drop: false,