fix: improves collections load
This commit is contained in:
parent
1aaab24688
commit
4b3d07d51c
@ -5,7 +5,7 @@ export default async function (model: FieldModel, options: any = {}) {
|
|||||||
const Collection = model.database.getModel('collections');
|
const Collection = model.database.getModel('collections');
|
||||||
if (model.get('interface') === 'subTable') {
|
if (model.get('interface') === 'subTable') {
|
||||||
const target = model.get('target');
|
const target = model.get('target');
|
||||||
if (target) {
|
if (target && !model.database.isDefined(target)) {
|
||||||
await Collection.import({
|
await Collection.import({
|
||||||
name: target,
|
name: target,
|
||||||
internal: true,
|
internal: true,
|
||||||
@ -16,11 +16,4 @@ export default async function (model: FieldModel, options: any = {}) {
|
|||||||
if (migrate) {
|
if (migrate) {
|
||||||
await model.migrate(options);
|
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')}});
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,4 @@ export default async function (model: FieldModel, options: any = {}) {
|
|||||||
if (migrate) {
|
if (migrate) {
|
||||||
await model.migrate(options);
|
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')}});
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ export function generateCollectionName(title?: string): string {
|
|||||||
export interface LoadOptions {
|
export interface LoadOptions {
|
||||||
reset?: boolean;
|
reset?: boolean;
|
||||||
where?: any;
|
where?: any;
|
||||||
|
skipExisting?: boolean;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,24 +60,25 @@ export class CollectionModel extends BaseModel {
|
|||||||
// table 是初始化和重新初始化
|
// table 是初始化和重新初始化
|
||||||
const table = this.database.table({...prevOptions, ...options});
|
const table = this.database.table({...prevOptions, ...options});
|
||||||
// 如果关系表未加载,一起处理
|
// 如果关系表未加载,一起处理
|
||||||
const associationTableNames = [];
|
// const associationTableNames = [];
|
||||||
for (const [key, association] of table.getAssociations()) {
|
// for (const [key, association] of table.getAssociations()) {
|
||||||
// TODO:是否需要考虑重载的情况?(暂时是跳过处理)
|
// // TODO:是否需要考虑重载的情况?(暂时是跳过处理)
|
||||||
// if (!this.database.isDefined(association.options.target)) {
|
// if (!this.database.isDefined(association.options.target)) {
|
||||||
// continue;
|
// continue;
|
||||||
// }
|
// }
|
||||||
associationTableNames.push(association.options.target);
|
// associationTableNames.push(association.options.target);
|
||||||
}
|
// }
|
||||||
if (associationTableNames.length) {
|
// console.log({associationTableNames});
|
||||||
await CollectionModel.load({
|
// if (associationTableNames.length) {
|
||||||
...opts,
|
// await CollectionModel.load({
|
||||||
where: {
|
// ...opts,
|
||||||
name: {
|
// where: {
|
||||||
[Op.in]: associationTableNames,
|
// name: {
|
||||||
}
|
// [Op.in]: associationTableNames,
|
||||||
}
|
// }
|
||||||
});
|
// }
|
||||||
}
|
// });
|
||||||
|
// }
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,12 +117,15 @@ export class CollectionModel extends BaseModel {
|
|||||||
* @param options
|
* @param options
|
||||||
*/
|
*/
|
||||||
static async load(options: LoadOptions = {}) {
|
static async load(options: LoadOptions = {}) {
|
||||||
const { reset = false, where = {}, transaction } = options;
|
const { skipExisting = false, reset = false, where = {}, transaction } = options;
|
||||||
const collections = await this.findAll({
|
const collections = await this.findAll({
|
||||||
transaction,
|
transaction,
|
||||||
where,
|
where,
|
||||||
});
|
});
|
||||||
for (const collection of collections) {
|
for (const collection of collections) {
|
||||||
|
if (skipExisting && this.database.isDefined(collection.get('name'))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
await collection.loadTableOptions({
|
await collection.loadTableOptions({
|
||||||
transaction,
|
transaction,
|
||||||
reset,
|
reset,
|
||||||
|
@ -31,12 +31,34 @@ export class FieldModel extends BaseModel {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
data = merge(...args);
|
data = merge(...args);
|
||||||
if (['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(data.type)) {
|
if (['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(data.type)) {
|
||||||
|
// 关系字段如果没有 name,相关参数都随机生成
|
||||||
if (!data.name) {
|
if (!data.name) {
|
||||||
data.name = generateFieldName();
|
data.name = generateFieldName();
|
||||||
|
// 通用,关系表
|
||||||
if (!data.target) {
|
if (!data.target) {
|
||||||
data.target = generateCollectionName();
|
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) {
|
if (!data.target) {
|
||||||
data.target = ['hasOne', 'belongsTo'].includes(data.type) ? Utils.pluralize(data.name) : data.name;
|
data.target = ['hasOne', 'belongsTo'].includes(data.type) ? Utils.pluralize(data.name) : data.name;
|
||||||
}
|
}
|
||||||
@ -77,24 +99,8 @@ export class FieldModel extends BaseModel {
|
|||||||
if (!collectionName) {
|
if (!collectionName) {
|
||||||
return false;
|
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)) {
|
if (!this.database.isDefined(collectionName)) {
|
||||||
await Collection.load({where: {name: collectionName}});
|
throw new Error(`${collectionName} is not defined`);
|
||||||
}
|
}
|
||||||
const table = this.database.getTable(collectionName);
|
const table = this.database.getTable(collectionName);
|
||||||
table.addField(await this.getOptions());
|
table.addField(await this.getOptions());
|
||||||
|
@ -33,11 +33,14 @@ export default async function (this: Application, options = {}) {
|
|||||||
Model.addHook(hookKey, hooks[modelName][hookKey]);
|
Model.addHook(hookKey, hooks[modelName][hookKey]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
try {
|
|
||||||
// 加载数据库表 collections 中已经保存的表配置
|
let initialized = false;
|
||||||
// 如果未 sync 可能报错
|
|
||||||
// TODO collections sync
|
this.use(async (ctx, next) => {
|
||||||
// await database.getModel('collections').load();
|
if (!initialized) {
|
||||||
} catch (error) {
|
await database.getModel('collections').load({skipExisting: true});
|
||||||
}
|
initialized = true;
|
||||||
|
}
|
||||||
|
await next();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ export default async function (options = {}) {
|
|||||||
Collection.addHook('afterUpdate', createCollectionPage);
|
Collection.addHook('afterUpdate', createCollectionPage);
|
||||||
Collection.addHook('afterDestroy', async (model, options) => {
|
Collection.addHook('afterDestroy', async (model, options) => {
|
||||||
const { transaction } = options;
|
const { transaction } = options;
|
||||||
console.log('afterDestroy', model);
|
// console.log('afterDestroy', model);
|
||||||
await Page.destroy({
|
await Page.destroy({
|
||||||
transaction,
|
transaction,
|
||||||
where: {
|
where: {
|
||||||
@ -127,7 +127,6 @@ export default async function (options = {}) {
|
|||||||
await transaction.commit();
|
await transaction.commit();
|
||||||
}
|
}
|
||||||
View.addHook('beforeValidate', (model) => {
|
View.addHook('beforeValidate', (model) => {
|
||||||
console.log('beforeValidate');
|
|
||||||
if (model.get('default')) {
|
if (model.get('default')) {
|
||||||
model.set('showInDataMenu', true);
|
model.set('showInDataMenu', true);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user