when the field migrate, load the collection options if is not in the database

This commit is contained in:
chenos 2020-12-11 10:37:43 +08:00
parent 880737c5c8
commit 3571f357cd
3 changed files with 8 additions and 22 deletions

View File

@ -78,7 +78,7 @@ export class BaseModel extends Model {
// 不处理关系数据 // 不处理关系数据
// @ts-ignore // @ts-ignore
if (_.get(this.constructor.associations, key)) { if (_.get(this.constructor.associations, key)) {
return this; return super.set(key, value, options);
} }
// 如果是 object 数据merge 处理 // 如果是 object 数据merge 处理
if (_.isPlainObject(value)) { if (_.isPlainObject(value)) {
@ -112,7 +112,7 @@ export class BaseModel extends Model {
// 不处理关系数据 // 不处理关系数据
// @ts-ignore // @ts-ignore
if (_.get(this.constructor.associations, key)) { if (_.get(this.constructor.associations, key)) {
return this; return super.setDataValue(key, value);
} }
if (_.isPlainObject(value)) { if (_.isPlainObject(value)) {
// @ts-ignore // @ts-ignore

View File

@ -35,12 +35,6 @@ export class CollectionModel extends BaseModel {
this.set('name', generateCollectionName()); this.set('name', generateCollectionName());
} }
generateNameIfNull() {
if (!this.get('name')) {
this.generateName();
}
}
/** /**
* name collection * name collection
* *
@ -109,9 +103,7 @@ export class CollectionModel extends BaseModel {
async getOptions(): Promise<TableOptions> { async getOptions(): Promise<TableOptions> {
return { return {
...this.get('options'), ...this.get(),
name: this.get('name'),
title: this.get('title'),
fields: await this.getFieldsOptions(), fields: await this.getFieldsOptions(),
}; };
} }

View File

@ -33,12 +33,6 @@ export class FieldModel extends BaseModel {
this.set('name', generateFieldName()); this.set('name', generateFieldName());
} }
generateNameIfNull() {
if (!this.get('name')) {
this.generateName();
}
}
setInterface(value) { setInterface(value) {
const { options } = types[value]; const { options } = types[value];
let args = []; let args = [];
@ -55,11 +49,7 @@ export class FieldModel extends BaseModel {
} }
async getOptions(): Promise<FieldOptions> { async getOptions(): Promise<FieldOptions> {
return { return this.get();
...this.get('options'),
type: this.get('type'),
name: this.get('name'),
};
} }
async migrate(options: any = {}) { async migrate(options: any = {}) {
@ -67,6 +57,10 @@ export class FieldModel extends BaseModel {
if (!collectionName) { if (!collectionName) {
return false; return false;
} }
// 如果 database 未定义load 出来
if (!this.database.isDefined(collectionName)) {
await this.database.getModel('collections').load({where: {name: collectionName}});
}
const table = this.database.getTable(collectionName); const table = this.database.getTable(collectionName);
table.addField(await this.getOptions()); table.addField(await this.getOptions());
await table.sync({ await table.sync({