From d2020fd6bc379cf1a2ca50d2d5833064ccf3714f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 24 Jul 2023 12:44:00 +0200 Subject: [PATCH] Move enrich views to sdk --- .../server/src/api/controllers/table/index.ts | 49 +---------------- packages/server/src/sdk/app/tables/index.ts | 53 ++++++++++++++++++- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index ab6c819a63..53202d6878 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -11,15 +11,11 @@ import { FetchTablesResponse, Table, TableResponse, - TableSchema, - TableViewsResponse, - UIFieldMetadata, UserCtx, } from "@budibase/types" import sdk from "../../../sdk" import { jsonFromCsvString } from "../../../utilities/csv" import { builderSocket } from "../../../websockets" -import _ from "lodash" function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { if (table && !tableId) { @@ -53,56 +49,15 @@ export async function fetch(ctx: UserCtx) { } }) - const response = [...internal, ...external].map(enrichTable) + const response = [...internal, ...external].map(sdk.tables.enrichViewSchemas) ctx.body = response } -function enrichSchema( - tableSchema: TableSchema, - viewOverrides: Record -) { - const result: TableSchema = {} - for (const [columnName, columnUIMetadata] of Object.entries(viewOverrides)) { - if (!columnUIMetadata.visible) { - continue - } - - if (!tableSchema[columnName]) { - continue - } - - result[columnName] = _.merge(tableSchema[columnName], columnUIMetadata) - } - return result -} - -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 || !Object.entries(v?.columns).length - ? table.schema - : enrichSchema(table.schema, v.columns), - } - } - return p - }, {} as TableViewsResponse), - } - - return result -} - export async function find(ctx: UserCtx) { const tableId = ctx.params.tableId const table = await sdk.tables.getTable(tableId) - ctx.body = enrichTable(table) + ctx.body = sdk.tables.enrichViewSchemas(table) } export async function save(ctx: UserCtx) { diff --git a/packages/server/src/sdk/app/tables/index.ts b/packages/server/src/sdk/app/tables/index.ts index 65010aeaa4..b94ca58538 100644 --- a/packages/server/src/sdk/app/tables/index.ts +++ b/packages/server/src/sdk/app/tables/index.ts @@ -5,9 +5,18 @@ import { isExternalTable, isSQL, } from "../../../integrations/utils" -import { Table, Database } from "@budibase/types" +import { + Table, + Database, + TableResponse, + TableViewsResponse, + TableSchema, + UIFieldMetadata, +} from "@budibase/types" import datasources from "../datasources" import { populateExternalTableSchemas, isEditableColumn } from "./validation" +import sdk from "../../../sdk" +import _ from "lodash" async function getAllInternalTables(db?: Database): Promise { if (!db) { @@ -55,6 +64,47 @@ async function getTable(tableId: any): Promise { } } +function enrichSchema( + tableSchema: TableSchema, + viewOverrides: Record +) { + const result: TableSchema = {} + for (const [columnName, columnUIMetadata] of Object.entries(viewOverrides)) { + if (!columnUIMetadata.visible) { + continue + } + + if (!tableSchema[columnName]) { + continue + } + + result[columnName] = _.merge(tableSchema[columnName], columnUIMetadata) + } + return result +} + +function enrichViewSchemas(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 || !Object.entries(v?.columns).length + ? table.schema + : enrichSchema(table.schema, v.columns), + } + } + return p + }, {} as TableViewsResponse), + } + + return result +} + export default { getAllInternalTables, getAllExternalTables, @@ -62,4 +112,5 @@ export default { getTable, populateExternalTableSchemas, isEditableColumn, + enrichViewSchemas, }