Merge pull request #11422 from Budibase/BUDI-7189/add_primary_column

Add primaryDisplay to ViewV2
This commit is contained in:
Adria Navarro 2023-08-02 14:08:23 +01:00 committed by GitHub
commit f556c428e5
4 changed files with 72 additions and 24 deletions

View File

@ -49,12 +49,18 @@ async function parseSchemaUI(ctx: Ctx, view: CreateViewRequest) {
const schemaUI =
view.schema &&
Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => {
p[fieldName] = {
const fieldSchema: RequiredKeys<UIFieldMetadata> = {
order: schemaValue.order,
width: schemaValue.width,
visible: schemaValue.visible,
icon: schemaValue.icon,
}
Object.entries(fieldSchema)
.filter(([_, val]) => val === undefined)
.forEach(([key]) => {
delete fieldSchema[key as keyof UIFieldMetadata]
})
p[fieldName] = fieldSchema
return p
}, {} as Record<string, RequiredKeys<UIFieldMetadata>>)
return schemaUI
@ -66,13 +72,14 @@ export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
const schemaUI = await parseSchemaUI(ctx, view)
const parsedView: Omit<ViewV2, "id" | "version"> = {
const parsedView: Omit<RequiredKeys<ViewV2>, "id" | "version"> = {
name: view.name,
tableId: view.tableId,
query: view.query,
sort: view.sort,
columns: view.schema && Object.keys(view.schema),
schemaUI,
primaryDisplay: view.primaryDisplay,
}
const result = await sdk.views.create(tableId, parsedView)
ctx.status = 201
@ -95,7 +102,7 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
const { tableId } = view
const schemaUI = await parseSchemaUI(ctx, view)
const parsedView: ViewV2 = {
const parsedView: RequiredKeys<ViewV2> = {
id: view.id,
name: view.name,
version: view.version,
@ -104,6 +111,7 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
sort: view.sort,
columns: view.schema && Object.keys(view.schema),
schemaUI,
primaryDisplay: view.primaryDisplay,
}
const result = await sdk.views.update(tableId, parsedView)

View File

@ -6,6 +6,7 @@ import {
SortOrder,
SortType,
Table,
UpdateViewRequest,
ViewV2,
} from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
@ -34,20 +35,6 @@ function priceTable(): Table {
describe("/v2/views", () => {
const config = setup.getConfig()
const viewFilters: Omit<CreateViewRequest, "name" | "tableId"> = {
query: { allOr: false, equal: { field: "value" } },
sort: {
field: "fieldToSort",
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
schema: {
name: {
visible: true,
},
},
}
afterAll(setup.afterAll)
beforeAll(async () => {
@ -70,20 +57,30 @@ describe("/v2/views", () => {
})
})
it("can persist views with queries", async () => {
const newView: CreateViewRequest = {
it("can persist views with all fields", async () => {
const newView: Required<CreateViewRequest> = {
name: generator.name(),
tableId: config.table!._id!,
query: viewFilters.query,
sort: viewFilters.sort,
primaryDisplay: generator.word(),
query: { allOr: false, equal: { field: "value" } },
sort: {
field: "fieldToSort",
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
schema: {
name: {
visible: true,
},
},
}
delete newView.schema
const res = await config.api.viewV2.create(newView)
expect(res).toEqual({
...newView,
query: viewFilters.query,
sort: viewFilters.sort,
schema: undefined,
columns: ["name"],
schemaUI: newView.schema,
id: expect.any(String),
version: 2,
})
@ -210,6 +207,46 @@ describe("/v2/views", () => {
})
})
it("can update all fields", async () => {
const tableId = config.table!._id!
const updatedData: Required<UpdateViewRequest> = {
version: view.version,
id: view.id,
tableId,
name: view.name,
primaryDisplay: generator.word(),
query: { equal: { [generator.word()]: generator.word() } },
sort: {
field: generator.word(),
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
schema: {
Category: {
visible: false,
},
},
}
await config.api.viewV2.update(updatedData)
expect(await config.api.table.get(tableId)).toEqual({
...config.table,
views: {
[view.name]: {
...updatedData,
schema: {
Category: expect.objectContaining({
visible: false,
}),
},
},
},
_rev: expect.any(String),
updatedAt: expect.any(String),
})
})
it("can update an existing view name", async () => {
const tableId = config.table!._id!
await config.api.viewV2.update({ ...view, name: "View B" })

View File

@ -86,6 +86,7 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) {
: schema[fieldName].order,
}
}
delete view.schemaUI
}
if (view?.columns?.length) {
@ -97,6 +98,7 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) {
pickedSchema[fieldName] = { ...schema[fieldName] }
}
schema = pickedSchema
delete view.columns
}
return {

View File

@ -18,6 +18,7 @@ export interface ViewV2 {
version: 2
id: string
name: string
primaryDisplay?: string
tableId: string
query?: SearchFilters
sort?: {