diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 9a4949bb43..b2a9e2f223 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -4,13 +4,22 @@ import { Ctx, UpdateViewRequest, ViewResponse, + ViewV2, } from "@budibase/types" export async function create(ctx: Ctx) { const view = ctx.request.body const { tableId } = view - const result = await sdk.views.create(tableId, view) + const parsedView: Omit = { + name: view.name, + tableId: view.tableId, + query: view.query, + sort: view.sort, + columns: view.schema && Object.keys(view.schema), + schemaUI: view.schema, + } + const result = await sdk.views.create(tableId, parsedView) ctx.status = 201 ctx.body = { data: result, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 2660864948..e2b81f4cf0 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -40,9 +40,10 @@ describe("/v2/views", () => { order: SortOrder.DESCENDING, type: SortType.STRING, }, - columns: ["name"], - schemaUI: { + schema: { name: { + name: "name", + type: FieldType.STRING, visible: true, }, }, @@ -74,17 +75,61 @@ describe("/v2/views", () => { const newView: CreateViewRequest = { name: generator.name(), tableId: config.table!._id!, - ...viewFilters, + query: viewFilters.query, + sort: viewFilters.sort, } + delete newView.schema const res = await config.api.viewV2.create(newView) expect(res).toEqual({ ...newView, - ...viewFilters, + query: viewFilters.query, + sort: viewFilters.sort, id: expect.any(String), version: 2, }) }) + + it("persist schema overrides", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: config.table!._id!, + schema: { + name: { + name: "name", + type: FieldType.STRING, + visible: true, + }, + lastname: { + name: "lastname", + type: FieldType.STRING, + visible: false, + }, + }, + } + + const createdView = await config.api.viewV2.create(newView) + + expect(await config.api.viewV2.get(createdView.id)).toEqual({ + ...newView, + schema: undefined, + columns: ["name", "lastname"], + schemaUI: { + name: { + name: "name", + type: FieldType.STRING, + visible: true, + }, + lastname: { + name: "lastname", + type: FieldType.STRING, + visible: false, + }, + }, + id: createdView.id, + version: 2, + }) + }) }) describe("update", () => { diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index fae0850f79..1f7fe6e2bb 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -3,6 +3,7 @@ import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" import { generator } from "@budibase/backend-core/tests" import { Response } from "superagent" +import sdk from "../../../sdk" export class ViewV2API extends TestAPI { constructor(config: TestConfiguration) { @@ -62,6 +63,12 @@ export class ViewV2API extends TestAPI { .expect(expectStatus) } + get = async (viewId: string) => { + return await this.config.doInContext(this.config.appId, () => + sdk.views.get(viewId) + ) + } + search = async ( viewId: string, options?: { diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts index 6b516c0314..b694521678 100644 --- a/packages/types/src/api/web/app/view.ts +++ b/packages/types/src/api/web/app/view.ts @@ -1,9 +1,12 @@ -import { TableSchema, ViewV2 } from "../../../documents" +import { ViewV2, FieldSchema } from "../../../documents" export interface ViewResponse { data: ViewV2 } -export type CreateViewRequest = Omit +export interface CreateViewRequest + extends Omit { + schema?: Record +} export type UpdateViewRequest = ViewV2