diff --git a/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts b/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts index 7eafd464b5..af2d37ed4a 100644 --- a/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts +++ b/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts @@ -679,7 +679,7 @@ export default abstract class BaseApiBuilder implements XcDynami const NC_VERSIONS = [ {name: '0009000', handler: null}, - {name: '0009044', handler: this.ncUpManyToMany} + {name: '0009044', handler: this.ncUpManyToMany.bind(this)} ] if (!await this.xcMeta?.knex?.schema?.hasTable?.('nc_store')) { return; @@ -808,6 +808,12 @@ export default abstract class BaseApiBuilder implements XcDynami await this.xcUpgrade(); } + public async onVirtualColumnAliasUpdate(tableName: string): Promise { + const model = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', {title: tableName}); + const meta = JSON.parse(model.meta); + this.models[tableName] = this.getBaseModel(meta); + } + protected async loadCommon(): Promise { this.baseLog(`loadCommon :`); @@ -1307,6 +1313,35 @@ export default abstract class BaseApiBuilder implements XcDynami } + protected async ncUpManyToMany(): Promise { + const models = await this.xcMeta.metaList(this.projectId, this.dbAlias, 'nc_models', { + fields: ['meta'] + }); + if (!models.length) { + return + } + const metas = []; + // add virtual columns for relations + for (const metaObj of models) { + const meta = JSON.parse(metaObj.meta); + metas.push(meta); + const ctx = this.generateContextForTable(meta.tn, meta.columns, [], meta.hasMany, meta.belongsTo, meta.type, meta._tn); + // generate virtual columns + meta.v = ModelXcMetaFactory.create(this.connectionConfig, {dir: '', ctx, filename: ''}).getVitualColumns(); + // set default primary values + ModelXcMetaFactory.create(this.connectionConfig, {}).mapDefaultPrimaryValue(meta.columns); + // update meta + await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { + meta: JSON.stringify(meta) + }, {title: meta.tn}) + } + + // generate many to many relations an columns + await this.getManyToManyRelations({localMetas: metas}); + return metas; + } + + private getColumnNameAlias(col, tableName?: string) { return this.metas?.[tableName]?.columns?.find(c => c.cn === col.cn)?._cn || col._cn || this.getInflectedName(col.cn, this.connectionConfig?.meta?.inflection?.cn); } @@ -1692,41 +1727,6 @@ export default abstract class BaseApiBuilder implements XcDynami } await this.loadXcAcl() } - - - protected async ncUpManyToMany(): Promise { - const models = await this.xcMeta.metaList(this.projectId, this.dbAlias, 'nc_models', { - fields: ['meta'] - }); - if (!models.length) { - return - } - const metas = []; - // add virtual columns for relations - for (const metaObj of models) { - const meta = JSON.parse(metaObj.meta); - metas.push(meta); - const ctx = this.generateContextForTable(meta.tn, meta.columns, [], meta.hasMany, meta.belongsTo, meta.type, meta._tn); - // generate virtual columns - meta.v = ModelXcMetaFactory.create(this.connectionConfig, {dir: '', ctx, filename: ''}).getVitualColumns(); - // set default primary values - ModelXcMetaFactory.create(this.connectionConfig, {}).mapDefaultPrimaryValue(meta.columns); - // update meta - await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { - meta: JSON.stringify(meta) - }, {title: meta.tn}) - } - - // generate many to many relations an columns - await this.getManyToManyRelations({localMetas: metas}); - return metas; - } - - public async onVirtualColumnAliasUpdate(tableName: string): Promise { - const model = await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_models', {title: tableName}); - const meta = JSON.parse(model.meta); - this.models[tableName] = this.getBaseModel(meta); - } } export {IGNORE_TABLES}; diff --git a/packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts b/packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts index 72e6aba6f6..f9717f4e8f 100644 --- a/packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts +++ b/packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts @@ -1900,7 +1900,10 @@ export class GqlApiBuilder extends BaseApiBuilder implements XcMetaMgr { /* generate gql schema of the table */ const schema = GqlXcSchemaFactory.create(this.connectionConfig, { dir: '', - ctx, + ctx: { + ...ctx, + manyToMany: meta.manyToMany + }, filename: '' }).getString();