Return view schema endpoint

This commit is contained in:
Adria Navarro 2023-07-20 09:45:49 +02:00
parent 4e646bb463
commit f3f0ee0959
6 changed files with 66 additions and 4 deletions

View File

@ -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<CreateViewRequest, ViewResponse>) {
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<void, ViewSchemaResponse>) {
const { viewId } = ctx.params
const schema = await sdk.views.getSchema(viewId)
ctx.body = { schema }
}

View File

@ -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" },
},
},
})
})
})
})

View File

@ -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(

View File

@ -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<void> {
delete table.views![view?.name]
await db.put(table)
}
export async function getSchema(viewId: string): Promise<TableSchema> {
const view = await get(viewId)
const table = await sdk.tables.getTable(view?.tableId)
return table.schema
}

View File

@ -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<ViewSchemaResponse> => {
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`)

View File

@ -1,7 +1,11 @@
import { ViewV2 } from "../../../documents"
import { TableSchema, ViewV2 } from "../../../documents"
export interface ViewResponse {
data: ViewV2
}
export type CreateViewRequest = Omit<ViewV2, "version" | "id">
export interface ViewSchemaResponse {
schema: TableSchema
}