From ed02aa4d8bca0d0dbca60b745599b332c1183e18 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 21 Jul 2023 13:45:33 +0200 Subject: [PATCH] Enrich view schemas --- .../server/src/api/controllers/table/index.ts | 52 ++++++++++++++----- packages/types/src/api/web/app/table.ts | 4 +- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 7450b3250e..42bfedda8b 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -7,7 +7,13 @@ import { } from "../../../utilities/schema" import { isExternalTable, isSQL } from "../../../integrations/utils" import { events } from "@budibase/backend-core" -import { FetchTablesResponse, Table, UserCtx } from "@budibase/types" +import { + FetchTablesResponse, + Table, + TableResponse, + TableViewsResponse, + UserCtx, +} from "@budibase/types" import sdk from "../../../sdk" import { jsonFromCsvString } from "../../../utilities/csv" import { builderSocket } from "../../../websockets" @@ -45,27 +51,47 @@ export async function fetch(ctx: UserCtx) { } }) - const tables = [...internal, ...external] + const response = [...internal, ...external].map(enrichTable) + ctx.body = response +} - for (const t of tables.filter(t => t.views)) { - for (const [viewName, view] of Object.entries(t.views!)) { - if (sdk.views.isV2(view)) { - t.views![viewName] = { - ...view, - schema: !view?.columns?.length - ? t.schema - : _.pick(t.schema, ...view.columns), +function enrichTable(table: Table): TableResponse { + const result: TableResponse = { + ...table, + views: Object.values(table.views ?? []).reduce((p, v) => { + if (!sdk.views.isV2(v)) { + p[v.name] = v + } else { + p[v.name] = { + ...v, + schema: !v?.columns?.length + ? table.schema + : _.pick(table.schema, ...v.columns), } } + return p + }, {} as TableViewsResponse), + } + + for (const [viewName, view] of Object.entries(table.views!)) { + if (sdk.views.isV2(view)) { + table.views![viewName] = { + ...view, + schema: !view?.columns?.length + ? table.schema + : _.pick(table.schema, ...view.columns), + } } } - ctx.body = tables as FetchTablesResponse + return result } -export async function find(ctx: UserCtx) { +export async function find(ctx: UserCtx) { const tableId = ctx.params.tableId - ctx.body = await sdk.tables.getTable(tableId) + const table = await sdk.tables.getTable(tableId) + + ctx.body = enrichTable(table) } export async function save(ctx: UserCtx) { diff --git a/packages/types/src/api/web/app/table.ts b/packages/types/src/api/web/app/table.ts index 2075d3b338..178f758254 100644 --- a/packages/types/src/api/web/app/table.ts +++ b/packages/types/src/api/web/app/table.ts @@ -4,9 +4,9 @@ interface ViewV2Response extends ViewV2 { schema: TableSchema } -type TableViewsResponse = { [key: string]: View | ViewV2Response } +export type TableViewsResponse = { [key: string]: View | ViewV2Response } -interface TableResponse extends Table { +export interface TableResponse extends Table { views?: TableViewsResponse }