fix: improves collections load

This commit is contained in:
chenos 2020-12-23 20:29:11 +08:00
parent 1aaab24688
commit 4b3d07d51c
6 changed files with 59 additions and 60 deletions

View File

@ -5,7 +5,7 @@ export default async function (model: FieldModel, options: any = {}) {
const Collection = model.database.getModel('collections');
if (model.get('interface') === 'subTable') {
const target = model.get('target');
if (target) {
if (target && !model.database.isDefined(target)) {
await Collection.import({
name: target,
internal: true,
@ -16,11 +16,4 @@ export default async function (model: FieldModel, options: any = {}) {
if (migrate) {
await model.migrate(options);
}
// if (model.get('collection_name') && model.get('parent_id')) {
// const parent = await model.getParent({
// ...options,
// });
// const Collection = model.database.getModel('collections');
// await Collection.load({...options, where: {name: parent.get('collection_name')}});
// }
}

View File

@ -5,11 +5,4 @@ export default async function (model: FieldModel, options: any = {}) {
if (migrate) {
await model.migrate(options);
}
// if (model.get('collection_name') && model.get('parent_id')) {
// const parent = await model.getParent({
// ...options,
// });
// const Collection = model.database.getModel('collections');
// await Collection.load({...options, where: {name: parent.get('collection_name')}});
// }
}

View File

@ -23,6 +23,7 @@ export function generateCollectionName(title?: string): string {
export interface LoadOptions {
reset?: boolean;
where?: any;
skipExisting?: boolean;
[key: string]: any;
}
@ -59,24 +60,25 @@ export class CollectionModel extends BaseModel {
// table 是初始化和重新初始化
const table = this.database.table({...prevOptions, ...options});
// 如果关系表未加载,一起处理
const associationTableNames = [];
for (const [key, association] of table.getAssociations()) {
// TODO是否需要考虑重载的情况暂时是跳过处理
// if (!this.database.isDefined(association.options.target)) {
// continue;
// }
associationTableNames.push(association.options.target);
}
if (associationTableNames.length) {
await CollectionModel.load({
...opts,
where: {
name: {
[Op.in]: associationTableNames,
}
}
});
}
// const associationTableNames = [];
// for (const [key, association] of table.getAssociations()) {
// // TODO是否需要考虑重载的情况暂时是跳过处理
// if (!this.database.isDefined(association.options.target)) {
// continue;
// }
// associationTableNames.push(association.options.target);
// }
// console.log({associationTableNames});
// if (associationTableNames.length) {
// await CollectionModel.load({
// ...opts,
// where: {
// name: {
// [Op.in]: associationTableNames,
// }
// }
// });
// }
return table;
}
@ -115,12 +117,15 @@ export class CollectionModel extends BaseModel {
* @param options
*/
static async load(options: LoadOptions = {}) {
const { reset = false, where = {}, transaction } = options;
const { skipExisting = false, reset = false, where = {}, transaction } = options;
const collections = await this.findAll({
transaction,
where,
});
for (const collection of collections) {
if (skipExisting && this.database.isDefined(collection.get('name'))) {
continue;
}
await collection.loadTableOptions({
transaction,
reset,

View File

@ -31,12 +31,34 @@ export class FieldModel extends BaseModel {
// @ts-ignore
data = merge(...args);
if (['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(data.type)) {
// 关系字段如果没有 name相关参数都随机生成
if (!data.name) {
data.name = generateFieldName();
// 通用,关系表
if (!data.target) {
data.target = generateCollectionName();
}
// 通用,外键
if (!data.foreignKey) {
data.foreignKey = generateFieldName();
}
if (data.type !== 'belongsTo' && !data.sourceKey) {
data.sourceKey = 'id';
}
if (['belongsTo', 'belongsToMany'].includes(data.type) && !data.targetKey) {
data.targetKey = 'id';
}
// 多对多关联
if (data.type === 'belongsToMany') {
if (!data.through) {
data.through = generateCollectionName();
}
if (!data.otherKey) {
data.otherKey = generateFieldName();
}
}
}
// 有 name但是没有 target
if (!data.target) {
data.target = ['hasOne', 'belongsTo'].includes(data.type) ? Utils.pluralize(data.name) : data.name;
}
@ -77,24 +99,8 @@ export class FieldModel extends BaseModel {
if (!collectionName) {
return false;
}
const Collection = this.database.getModel('collections');
if (this.get('interface') === 'linkTo') {
// afterCreate 时 target 表不知道为什么丢失了,需要重新加载
const target = this.get('target');
if (!this.database.isDefined(target)) {
await Collection.load({
...options,
where: {
name: target,
}
});
}
// const table = this.getTable(target);
// console.log(model.get('target'), typeof table);
}
// 如果 database 未定义load 出来
if (!this.database.isDefined(collectionName)) {
await Collection.load({where: {name: collectionName}});
throw new Error(`${collectionName} is not defined`);
}
const table = this.database.getTable(collectionName);
table.addField(await this.getOptions());

View File

@ -33,11 +33,14 @@ export default async function (this: Application, options = {}) {
Model.addHook(hookKey, hooks[modelName][hookKey]);
});
});
try {
// 加载数据库表 collections 中已经保存的表配置
// 如果未 sync 可能报错
// TODO collections sync
// await database.getModel('collections').load();
} catch (error) {
}
let initialized = false;
this.use(async (ctx, next) => {
if (!initialized) {
await database.getModel('collections').load({skipExisting: true});
initialized = true;
}
await next();
});
}

View File

@ -68,7 +68,7 @@ export default async function (options = {}) {
Collection.addHook('afterUpdate', createCollectionPage);
Collection.addHook('afterDestroy', async (model, options) => {
const { transaction } = options;
console.log('afterDestroy', model);
// console.log('afterDestroy', model);
await Page.destroy({
transaction,
where: {
@ -127,7 +127,6 @@ export default async function (options = {}) {
await transaction.commit();
}
View.addHook('beforeValidate', (model) => {
console.log('beforeValidate');
if (model.get('default')) {
model.set('showInDataMenu', true);
}