Change viewrequest to accept schema
This commit is contained in:
parent
73ded07484
commit
b528257bbe
|
@ -4,13 +4,22 @@ import {
|
||||||
Ctx,
|
Ctx,
|
||||||
UpdateViewRequest,
|
UpdateViewRequest,
|
||||||
ViewResponse,
|
ViewResponse,
|
||||||
|
ViewV2,
|
||||||
} from "@budibase/types"
|
} 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
|
||||||
const { tableId } = view
|
const { tableId } = view
|
||||||
|
|
||||||
const result = await sdk.views.create(tableId, view)
|
const parsedView: Omit<ViewV2, "id" | "version"> = {
|
||||||
|
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.status = 201
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: result,
|
data: result,
|
||||||
|
|
|
@ -40,9 +40,10 @@ describe("/v2/views", () => {
|
||||||
order: SortOrder.DESCENDING,
|
order: SortOrder.DESCENDING,
|
||||||
type: SortType.STRING,
|
type: SortType.STRING,
|
||||||
},
|
},
|
||||||
columns: ["name"],
|
schema: {
|
||||||
schemaUI: {
|
|
||||||
name: {
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
visible: true,
|
visible: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -74,17 +75,61 @@ describe("/v2/views", () => {
|
||||||
const newView: CreateViewRequest = {
|
const newView: CreateViewRequest = {
|
||||||
name: generator.name(),
|
name: generator.name(),
|
||||||
tableId: config.table!._id!,
|
tableId: config.table!._id!,
|
||||||
...viewFilters,
|
query: viewFilters.query,
|
||||||
|
sort: viewFilters.sort,
|
||||||
}
|
}
|
||||||
|
delete newView.schema
|
||||||
const res = await config.api.viewV2.create(newView)
|
const res = await config.api.viewV2.create(newView)
|
||||||
|
|
||||||
expect(res).toEqual({
|
expect(res).toEqual({
|
||||||
...newView,
|
...newView,
|
||||||
...viewFilters,
|
query: viewFilters.query,
|
||||||
|
sort: viewFilters.sort,
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
version: 2,
|
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", () => {
|
describe("update", () => {
|
||||||
|
|
|
@ -3,6 +3,7 @@ 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"
|
||||||
import { Response } from "superagent"
|
import { Response } from "superagent"
|
||||||
|
import sdk from "../../../sdk"
|
||||||
|
|
||||||
export class ViewV2API extends TestAPI {
|
export class ViewV2API extends TestAPI {
|
||||||
constructor(config: TestConfiguration) {
|
constructor(config: TestConfiguration) {
|
||||||
|
@ -62,6 +63,12 @@ export class ViewV2API extends TestAPI {
|
||||||
.expect(expectStatus)
|
.expect(expectStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get = async (viewId: string) => {
|
||||||
|
return await this.config.doInContext(this.config.appId, () =>
|
||||||
|
sdk.views.get(viewId)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
search = async (
|
search = async (
|
||||||
viewId: string,
|
viewId: string,
|
||||||
options?: {
|
options?: {
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
import { TableSchema, ViewV2 } from "../../../documents"
|
import { ViewV2, FieldSchema } from "../../../documents"
|
||||||
|
|
||||||
export interface ViewResponse {
|
export interface ViewResponse {
|
||||||
data: ViewV2
|
data: ViewV2
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CreateViewRequest = Omit<ViewV2, "version" | "id">
|
export interface CreateViewRequest
|
||||||
|
extends Omit<ViewV2, "version" | "id" | "columns" | "schemaUI"> {
|
||||||
|
schema?: Record<string, FieldSchema>
|
||||||
|
}
|
||||||
|
|
||||||
export type UpdateViewRequest = ViewV2
|
export type UpdateViewRequest = ViewV2
|
||||||
|
|
Loading…
Reference in New Issue