From f3f0ee095966b469aa9bd7f7c8176855a6f25914 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 20 Jul 2023 09:45:49 +0200 Subject: [PATCH] Return view schema endpoint --- .../src/api/controllers/view/viewsV2.ts | 14 ++++++++++- .../src/api/routes/tests/viewV2.spec.ts | 23 +++++++++++++++++++ packages/server/src/api/routes/view.ts | 5 ++++ packages/server/src/sdk/app/views/index.ts | 9 +++++++- .../server/src/tests/utilities/api/viewV2.ts | 13 ++++++++++- packages/types/src/api/web/app/view.ts | 6 ++++- 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 94e53e52fb..a6d53a1d81 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -1,5 +1,10 @@ import sdk from "../../../sdk" -import { CreateViewRequest, Ctx, ViewResponse } from "@budibase/types" +import { + CreateViewRequest, + Ctx, + ViewResponse, + ViewSchemaResponse, +} from "@budibase/types" export async function create(ctx: Ctx) { const view = ctx.request.body @@ -18,3 +23,10 @@ export async function remove(ctx: Ctx) { await sdk.views.remove(viewId) ctx.status = 204 } + +export async function getSchema(ctx: Ctx) { + const { viewId } = ctx.params + + const schema = await sdk.views.getSchema(viewId) + ctx.body = { schema } +} diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 480580eb86..35a36c38f7 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -106,4 +106,27 @@ describe("/v2/views", () => { expect(await getPersistedView()).toBeUndefined() }) }) + + describe("getSchema", () => { + let view: ViewV2 + + beforeAll(async () => { + await config.createTable(priceTable()) + view = await config.api.viewV2.create() + }) + + it("returns table schema if no columns are defined", async () => { + const result = await config.api.viewV2.getSchema(view.id) + expect(result).toEqual({ + schema: { + Price: { type: "number", name: "Price", constraints: {} }, + Category: { + type: "string", + name: "Category", + constraints: { type: "string" }, + }, + }, + }) + }) + }) }) diff --git a/packages/server/src/api/routes/view.ts b/packages/server/src/api/routes/view.ts index f8ae4abf0d..53b556ed76 100644 --- a/packages/server/src/api/routes/view.ts +++ b/packages/server/src/api/routes/view.ts @@ -18,6 +18,11 @@ router authorized(permissions.BUILDER), viewController.v2.remove ) + .get( + `/api/v2/views/:viewId/schema`, + authorized(permissions.BUILDER), + viewController.v2.getSchema + ) router .get( diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index e4d6f061eb..5b11389e9a 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -1,5 +1,5 @@ import { HTTPError, context } from "@budibase/backend-core" -import { View, ViewV2 } from "@budibase/types" +import { TableSchema, View, ViewV2 } from "@budibase/types" import sdk from "../../../sdk" import * as utils from "../../../db/utils" @@ -48,3 +48,10 @@ export async function remove(viewId: string): Promise { delete table.views![view?.name] await db.put(table) } + +export async function getSchema(viewId: string): Promise { + const view = await get(viewId) + const table = await sdk.tables.getTable(view?.tableId) + + return table.schema +} diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index d79f324d85..bcc0eb56c9 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -1,4 +1,4 @@ -import { ViewV2 } from "@budibase/types" +import { ViewSchemaResponse, ViewV2 } from "@budibase/types" import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" import { generator } from "@budibase/backend-core/tests" @@ -38,6 +38,17 @@ export class ViewV2API extends TestAPI { .expect(expectStatus) } + getSchema = async ( + viewId: string, + { expectStatus } = { expectStatus: 200 } + ): Promise => { + const res = await this.request + .get(`/api/v2/views/${viewId}/schema`) + .set(this.config.defaultHeaders()) + .expect(expectStatus) + return res.body + } + search = async (viewId: string, { expectStatus } = { expectStatus: 200 }) => { return this.request .get(`/api/v2/views/${viewId}/search`) diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts index 2cd371a025..11df32dec1 100644 --- a/packages/types/src/api/web/app/view.ts +++ b/packages/types/src/api/web/app/view.ts @@ -1,7 +1,11 @@ -import { ViewV2 } from "../../../documents" +import { TableSchema, ViewV2 } from "../../../documents" export interface ViewResponse { data: ViewV2 } export type CreateViewRequest = Omit + +export interface ViewSchemaResponse { + schema: TableSchema +}