Remove view endpoint

This commit is contained in:
Adria Navarro 2023-07-21 12:30:33 +02:00
parent 22dd218b1a
commit 3f2fa1a8dc
6 changed files with 70 additions and 115 deletions

View File

@ -1,10 +1,5 @@
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { import { CreateViewRequest, Ctx, ViewResponse } from "@budibase/types"
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
@ -23,10 +18,3 @@ 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 }
}

View File

@ -112,74 +112,74 @@ describe("/v2/views", () => {
}) })
}) })
describe("getSchema", () => { // describe("getSchema", () => {
beforeAll(async () => { // beforeAll(async () => {
await config.createTable(priceTable()) // await config.createTable(priceTable())
}) // })
it("returns table schema if no columns are defined", async () => { // it("returns table schema if no columns are defined", async () => {
const view = await config.api.viewV2.create() // const view = await config.api.viewV2.create()
const result = await config.api.viewV2.getSchema(view.id) // const result = await config.api.viewV2.getSchema(view.id)
expect(result).toEqual({ // expect(result).toEqual({
schema: { // schema: {
Price: { // Price: {
type: "number", // type: "number",
name: "Price", // name: "Price",
constraints: {}, // constraints: {},
}, // },
Currency: { // Currency: {
type: "string", // type: "string",
name: "Currency", // name: "Currency",
constraints: {}, // constraints: {},
}, // },
ItemId: { // ItemId: {
type: "string", // type: "string",
name: "ItemId", // name: "ItemId",
constraints: { // constraints: {
type: "string", // type: "string",
}, // },
}, // },
}, // },
}) // })
}) // })
it("respects view column definition if exists", async () => { // it("respects view column definition if exists", async () => {
const view = await config.api.viewV2.create({ // const view = await config.api.viewV2.create({
columns: ["Price", "ItemId"], // columns: ["Price", "ItemId"],
}) // })
const result = await config.api.viewV2.getSchema(view.id) // const result = await config.api.viewV2.getSchema(view.id)
expect(result).toEqual({ // expect(result).toEqual({
schema: { // schema: {
Price: { // Price: {
type: "number", // type: "number",
name: "Price", // name: "Price",
constraints: {}, // constraints: {},
}, // },
ItemId: { // ItemId: {
type: "string", // type: "string",
name: "ItemId", // name: "ItemId",
constraints: { // constraints: {
type: "string", // type: "string",
}, // },
}, // },
}, // },
}) // })
}) // })
it("respects view column definition if exists", async () => { // it("respects view column definition if exists", async () => {
const view = await config.api.viewV2.create({ // const view = await config.api.viewV2.create({
columns: ["Price", "innexistingColumn"], // columns: ["Price", "innexistingColumn"],
}) // })
const result = await config.api.viewV2.getSchema(view.id) // const result = await config.api.viewV2.getSchema(view.id)
expect(result).toEqual({ // expect(result).toEqual({
schema: { // schema: {
Price: { // Price: {
type: "number", // type: "number",
name: "Price", // name: "Price",
constraints: {}, // constraints: {},
}, // },
}, // },
}) // })
}) // })
}) // })
}) })

View File

@ -18,11 +18,6 @@ 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(

View File

@ -1,9 +1,8 @@
import { HTTPError, context } from "@budibase/backend-core" import { HTTPError, context } from "@budibase/backend-core"
import { TableSchema, View, ViewV2 } from "@budibase/types" import { 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"
import _ from "lodash"
export async function get(viewId: string): Promise<ViewV2 | undefined> { export async function get(viewId: string): Promise<ViewV2 | undefined> {
const { tableId } = utils.extractViewInfoFromID(viewId) const { tableId } = utils.extractViewInfoFromID(viewId)
@ -49,15 +48,3 @@ 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)
if (!view?.columns?.length) {
return table.schema
}
const schema = _.pick(table.schema, ...view.columns)
return schema
}

View File

@ -1,4 +1,4 @@
import { ViewSchemaResponse, ViewV2 } from "@budibase/types" import { 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,17 +38,6 @@ 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`)

View File

@ -5,7 +5,3 @@ export interface ViewResponse {
} }
export type CreateViewRequest = Omit<ViewV2, "version" | "id"> export type CreateViewRequest = Omit<ViewV2, "version" | "id">
export interface ViewSchemaResponse {
schema: TableSchema
}