From 1d7027f67ac739053a9f29d04c080a5a4b7ae573 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 31 May 2023 11:39:17 +0530 Subject: [PATCH] fix: handle if lookup column is LTAR Signed-off-by: Pranav C --- .../src/services/public-metas.service.ts | 77 ++++++++++++------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/packages/nocodb/src/services/public-metas.service.ts b/packages/nocodb/src/services/public-metas.service.ts index cb4e3664a4..0073502bba 100644 --- a/packages/nocodb/src/services/public-metas.service.ts +++ b/packages/nocodb/src/services/public-metas.service.ts @@ -63,22 +63,7 @@ export class PublicMetasService { // load related table metas for (const col of view.model.columns) { - if (UITypes.LinkToAnotherRecord === col.uidt) { - const colOpt = await col.getColOptions(); - relatedMetas[colOpt.fk_related_model_id] = await Model.getWithInfo({ - id: colOpt.fk_related_model_id, - }); - if (colOpt.type === 'mm') { - relatedMetas[colOpt.fk_mm_model_id] = await Model.getWithInfo({ - id: colOpt.fk_mm_model_id, - }); - } - } else if (UITypes.Lookup === col.uidt) { - await this.extractLookupRelatedMetas({ - lookupColOption: await col.getColOptions(), - relatedMetas, - }); - } + await this.extractRelatedMetas({ col, relatedMetas }); } view.relatedMetas = relatedMetas; @@ -86,6 +71,43 @@ export class PublicMetasService { return view; } + private async extractRelatedMetas({ + col, + relatedMetas = {}, + }: { + col: Column; + relatedMetas: Record; + }) { + if (UITypes.LinkToAnotherRecord === col.uidt) { + await this.extractLTARRelatedMetas({ + ltarColOption: await col.getColOptions(), + relatedMetas, + }); + } else if (UITypes.Lookup === col.uidt) { + await this.extractLookupRelatedMetas({ + lookupColOption: await col.getColOptions(), + relatedMetas, + }); + } + } + + private async extractLTARRelatedMetas({ + ltarColOption, + relatedMetas = {}, + }: { + ltarColOption: LinkToAnotherRecordColumn; + relatedMetas: { [key: string]: Model }; + }) { + relatedMetas[ltarColOption.fk_related_model_id] = await Model.getWithInfo({ + id: ltarColOption.fk_related_model_id, + }); + if (ltarColOption.type === 'mm') { + relatedMetas[ltarColOption.fk_mm_model_id] = await Model.getWithInfo({ + id: ltarColOption.fk_mm_model_id, + }); + } + } + private async extractLookupRelatedMetas({ lookupColOption, relatedMetas = {}, @@ -96,7 +118,7 @@ export class PublicMetasService { const relationCol = await Column.get({ colId: lookupColOption.fk_relation_column_id, }); - const lookupCol = await Column.get({ + const lookedUpCol = await Column.get({ colId: lookupColOption.fk_lookup_column_id, }); @@ -104,24 +126,23 @@ export class PublicMetasService { // if not already extracted if (!relatedMetas[relationCol.fk_model_id]) { relatedMetas[relationCol.fk_model_id] = await Model.getWithInfo({ - id: lookupCol.fk_model_id, + id: relationCol.fk_model_id, }); } + // extract meta for table in which looked up column belongs // if not already extracted - if (!relatedMetas[lookupCol.fk_model_id]) { - relatedMetas[lookupCol.fk_model_id] = await Model.getWithInfo({ - id: lookupCol.fk_model_id, + if (!relatedMetas[lookedUpCol.fk_model_id]) { + relatedMetas[lookedUpCol.fk_model_id] = await Model.getWithInfo({ + id: lookedUpCol.fk_model_id, }); } - // if looked up column is a lookup column do the same for it by recursion - if (lookupCol.uidt === UITypes.Lookup) { - await this.extractLookupRelatedMetas({ - lookupColOption: await lookupCol.getColOptions(), - relatedMetas, - }); - } + // extract metas related to the looked up column + await this.extractRelatedMetas({ + col: lookedUpCol, + relatedMetas, + }); } async publicSharedBaseGet(param: { sharedBaseUuid: string }): Promise {