Return view schema endpoint
This commit is contained in:
parent
4e646bb463
commit
f3f0ee0959
|
@ -1,5 +1,10 @@
|
||||||
import sdk from "../../../sdk"
|
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>) {
|
export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
|
||||||
const view = ctx.request.body
|
const view = ctx.request.body
|
||||||
|
@ -18,3 +23,10 @@ export async function remove(ctx: Ctx) {
|
||||||
await sdk.views.remove(viewId)
|
await sdk.views.remove(viewId)
|
||||||
ctx.status = 204
|
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 }
|
||||||
|
}
|
||||||
|
|
|
@ -106,4 +106,27 @@ describe("/v2/views", () => {
|
||||||
expect(await getPersistedView()).toBeUndefined()
|
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" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -18,6 +18,11 @@ router
|
||||||
authorized(permissions.BUILDER),
|
authorized(permissions.BUILDER),
|
||||||
viewController.v2.remove
|
viewController.v2.remove
|
||||||
)
|
)
|
||||||
|
.get(
|
||||||
|
`/api/v2/views/:viewId/schema`,
|
||||||
|
authorized(permissions.BUILDER),
|
||||||
|
viewController.v2.getSchema
|
||||||
|
)
|
||||||
|
|
||||||
router
|
router
|
||||||
.get(
|
.get(
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { HTTPError, context } from "@budibase/backend-core"
|
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 sdk from "../../../sdk"
|
||||||
import * as utils from "../../../db/utils"
|
import * as utils from "../../../db/utils"
|
||||||
|
@ -48,3 +48,10 @@ export async function remove(viewId: string): Promise<void> {
|
||||||
delete table.views![view?.name]
|
delete table.views![view?.name]
|
||||||
await db.put(table)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { ViewV2 } from "@budibase/types"
|
import { ViewSchemaResponse, ViewV2 } from "@budibase/types"
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import TestConfiguration from "../TestConfiguration"
|
||||||
import { TestAPI } from "./base"
|
import { TestAPI } from "./base"
|
||||||
import { generator } from "@budibase/backend-core/tests"
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
|
@ -38,6 +38,17 @@ export class ViewV2API extends TestAPI {
|
||||||
.expect(expectStatus)
|
.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 }) => {
|
search = async (viewId: string, { expectStatus } = { expectStatus: 200 }) => {
|
||||||
return this.request
|
return this.request
|
||||||
.get(`/api/v2/views/${viewId}/search`)
|
.get(`/api/v2/views/${viewId}/search`)
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
import { ViewV2 } from "../../../documents"
|
import { TableSchema, ViewV2 } from "../../../documents"
|
||||||
|
|
||||||
export interface ViewResponse {
|
export interface ViewResponse {
|
||||||
data: ViewV2
|
data: ViewV2
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CreateViewRequest = Omit<ViewV2, "version" | "id">
|
export type CreateViewRequest = Omit<ViewV2, "version" | "id">
|
||||||
|
|
||||||
|
export interface ViewSchemaResponse {
|
||||||
|
schema: TableSchema
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue