From 22dd218b1ae0259f27ebc725035d0ae7f727551a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 21 Jul 2023 12:30:21 +0200 Subject: [PATCH] Fetch schema --- .../server/src/api/controllers/table/index.ts | 18 ++++++++- .../server/src/api/routes/tests/table.spec.ts | 38 ++++++++++++++++++- .../server/src/tests/utilities/api/table.ts | 11 ++++++ packages/types/src/api/web/app/table.ts | 14 ++++++- 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index c2cdbbb06c..7450b3250e 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -11,6 +11,7 @@ import { FetchTablesResponse, Table, 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) { @@ -44,7 +45,22 @@ export async function fetch(ctx: UserCtx) { } }) - ctx.body = [...internal, ...external] + const tables = [...internal, ...external] + + 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), + } + } + } + } + + ctx.body = tables as FetchTablesResponse } export async function find(ctx: UserCtx) { diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index f5703681b5..b8520760a7 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -228,13 +228,49 @@ describe("/tables", () => { expect.objectContaining({ _id: tableId, views: views.reduce((p, c) => { - p[c.name] = c + p[c.name] = { ...c, schema: expect.anything() } return p }, {} as any), }), ]) ) }) + + it("should fetch the default schema if not overriden", async () => { + const tableId = config.table!._id! + const view = await config.api.viewV2.create({ tableId }) + + const res = await config.api.table.fetch() + + expect(res).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + _id: tableId, + views: { + [view.name]: { + ...view, + schema: { + name: { + type: "string", + name: "name", + constraints: { + type: "string", + }, + }, + description: { + type: "string", + name: "description", + constraints: { + type: "string", + }, + }, + }, + }, + }, + }), + ]) + ) + }) }) describe("indexing", () => { diff --git a/packages/server/src/tests/utilities/api/table.ts b/packages/server/src/tests/utilities/api/table.ts index 7e3ef6efa4..70f0869650 100644 --- a/packages/server/src/tests/utilities/api/table.ts +++ b/packages/server/src/tests/utilities/api/table.ts @@ -7,6 +7,17 @@ export class TableAPI extends TestAPI { super(config) } + fetch = async ( + { expectStatus } = { expectStatus: 200 } + ): Promise => { + const res = await this.request + .get(`/api/tables`) + .set(this.config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(expectStatus) + return res.body + } + get = async ( tableId: string, { expectStatus } = { expectStatus: 200 } diff --git a/packages/types/src/api/web/app/table.ts b/packages/types/src/api/web/app/table.ts index 1044398500..2075d3b338 100644 --- a/packages/types/src/api/web/app/table.ts +++ b/packages/types/src/api/web/app/table.ts @@ -1,3 +1,13 @@ -import { Table } from "../../../documents" +import { Table, TableSchema, View, ViewV2 } from "../../../documents" -export type FetchTablesResponse = Table[] +interface ViewV2Response extends ViewV2 { + schema: TableSchema +} + +type TableViewsResponse = { [key: string]: View | ViewV2Response } + +interface TableResponse extends Table { + views?: TableViewsResponse +} + +export type FetchTablesResponse = TableResponse[]