1
0
Fork 0
mirror of synced 2024-10-03 19:43:32 +13:00

Removing concept of columns and schemaUI, replacing with just schema as now the backend uses UI attributes.

This commit is contained in:
mike12345567 2023-08-11 15:52:13 +01:00
parent 199d27fc89
commit 9e0964a5e3
8 changed files with 75 additions and 103 deletions

View file

@ -27,8 +27,8 @@ export async function searchView(
const table = await sdk.tables.getTable(view?.tableId)
const viewFields =
(view.columns &&
Object.entries(view.columns).length &&
(view.schema &&
Object.entries(view.schema).length &&
Object.keys(sdk.views.enrichSchema(view, table.schema).schema)) ||
undefined

View file

@ -10,7 +10,7 @@ import {
} from "@budibase/types"
import { builderSocket } from "../../../websockets"
async function parseSchemaUI(ctx: Ctx, view: CreateViewRequest) {
async function parseSchema(ctx: Ctx, view: CreateViewRequest) {
if (!view.schema) {
return
}
@ -74,15 +74,14 @@ export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
const view = ctx.request.body
const { tableId } = view
const schemaUI = await parseSchemaUI(ctx, view)
const schema = await parseSchema(ctx, view)
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,
schema,
primaryDisplay: view.primaryDisplay,
}
const result = await sdk.views.create(tableId, parsedView)
@ -108,7 +107,7 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
const { tableId } = view
const schemaUI = await parseSchemaUI(ctx, view)
const schema = await parseSchema(ctx, view)
const parsedView: RequiredKeys<ViewV2> = {
id: view.id,
name: view.name,
@ -116,8 +115,7 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
tableId: view.tableId,
query: view.query,
sort: view.sort,
columns: view.schema && Object.keys(view.schema),
schemaUI,
schema,
primaryDisplay: view.primaryDisplay,
}

View file

@ -78,9 +78,7 @@ describe("/v2/views", () => {
expect(res).toEqual({
...newView,
schema: undefined,
columns: ["name"],
schemaUI: newView.schema,
schema: newView.schema,
id: expect.any(String),
version: 2,
})
@ -111,9 +109,7 @@ describe("/v2/views", () => {
expect(await config.api.viewV2.get(createdView.id)).toEqual({
...newView,
schema: undefined,
columns: ["Price", "Category"],
schemaUI: {
schema: {
Price: {
visible: true,
order: 1,
@ -240,9 +236,13 @@ describe("/v2/views", () => {
[view.name]: {
...updatedData,
schema: {
...config.table!.schema,
Category: expect.objectContaining({
visible: false,
}),
Price: expect.objectContaining({
visible: false,
}),
},
},
},
@ -361,9 +361,7 @@ describe("/v2/views", () => {
expect(await config.api.viewV2.get(view.id)).toEqual({
...view,
schema: undefined,
columns: ["Price", "Category"],
schemaUI: {
schema: {
Price: {
visible: true,
order: 1,
@ -459,7 +457,7 @@ describe("/v2/views", () => {
}
const res = await config.api.viewV2.create(newView)
const view = await config.api.viewV2.get(res.id)
expect(view!.schemaUI?.Price).toBeUndefined()
expect(view!.schema?.Price).toBeUndefined()
const updatedTable = await config.getTable(table._id!)
const viewSchema = updatedTable.views[view!.name!].schema
expect(viewSchema.Price.visible).toEqual(false)

View file

@ -42,7 +42,7 @@ export async function trimViewFields<T extends Row>(
data: T
): Promise<T> {
const view = await sdk.views.get(viewId)
if (!view?.columns || !Object.keys(view.columns).length) {
if (!view?.schema || !Object.keys(view.schema).length) {
return data
}

View file

@ -79,30 +79,23 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) {
}
let schema = { ...tableSchema }
const anyViewOrder = Object.values(view.schemaUI || {}).some(
const anyViewOrder = Object.values(view.schema || {}).some(
ui => ui.order != null
)
for (const key of Object.keys(schema)) {
// if nothing specified in view, then it is not visible
const ui = view.schemaUI?.[key] || { visible: false }
schema[key] = {
...schema[key],
...ui,
order: anyViewOrder ? ui?.order ?? undefined : schema[key].order,
}
}
delete view.schemaUI
if (view?.columns?.length) {
const pickedSchema: Record<string, FieldSchema> = {}
for (const fieldName of view.columns) {
if (!schema[fieldName]) {
continue
if (Object.keys(view.schema || {}).length > 0) {
for (const key of Object.keys(schema)) {
// if nothing specified in view, then it is not visible
const ui = view.schema?.[key] || { visible: false }
if (ui.visible === false) {
schema[key].visible = false
} else {
schema[key] = {
...schema[key],
...ui,
order: anyViewOrder ? ui?.order ?? undefined : schema[key].order,
}
}
pickedSchema[fieldName] = { ...schema[fieldName] }
}
schema = pickedSchema
delete view.columns
}
return {
@ -116,31 +109,23 @@ export function syncSchema(
schema: TableSchema,
renameColumn: RenameColumn | undefined
): ViewV2 {
if (renameColumn) {
if (view.columns) {
view.columns[view.columns.indexOf(renameColumn.old)] =
renameColumn.updated
}
if (view.schemaUI) {
view.schemaUI[renameColumn.updated] = view.schemaUI[renameColumn.old]
delete view.schemaUI[renameColumn.old]
}
if (renameColumn && view.schema) {
view.schema[renameColumn.updated] = view.schema[renameColumn.old]
delete view.schema[renameColumn.old]
}
if (view.schemaUI) {
for (const fieldName of Object.keys(view.schemaUI)) {
if (view.schema) {
for (const fieldName of Object.keys(view.schema)) {
if (!schema[fieldName]) {
delete view.schemaUI[fieldName]
delete view.schema[fieldName]
}
}
for (const fieldName of Object.keys(schema)) {
if (!view.schemaUI[fieldName]) {
view.schemaUI[fieldName] = { visible: false }
if (!view.schema[fieldName]) {
view.schema[fieldName] = { visible: false }
}
}
}
view.columns = view.columns?.filter(x => schema[x])
return view
}

View file

@ -110,7 +110,10 @@ describe("table sdk", () => {
id: generator.guid(),
name: generator.guid(),
tableId,
columns: ["name", "id"],
schema: {
name: { visible: true },
id: { visible: true },
},
}
const res = enrichSchema(view, basicTable.schema)
@ -118,6 +121,7 @@ describe("table sdk", () => {
expect(res).toEqual({
...view,
schema: {
...basicTable.schema,
name: {
type: "string",
name: "name",
@ -148,7 +152,10 @@ describe("table sdk", () => {
id: generator.guid(),
name: generator.guid(),
tableId,
columns: ["unnexisting", "name"],
schema: {
unnexisting: { visible: true },
name: { visible: true },
},
}
const res = enrichSchema(view, basicTable.schema)
@ -157,6 +164,7 @@ describe("table sdk", () => {
expect.objectContaining({
...view,
schema: {
...basicTable.schema,
name: {
type: "string",
name: "name",
@ -179,8 +187,7 @@ describe("table sdk", () => {
id: generator.guid(),
name: generator.guid(),
tableId,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },
@ -193,6 +200,7 @@ describe("table sdk", () => {
expect.objectContaining({
...view,
schema: {
...basicTable.schema,
name: {
type: "string",
name: "name",
@ -234,11 +242,10 @@ describe("table sdk", () => {
id: generator.guid(),
name: generator.guid(),
tableId,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, order: 1 },
id: { visible: true },
description: { visible: false, order: 2 },
description: { visible: true, order: 2 },
},
}
@ -248,6 +255,7 @@ describe("table sdk", () => {
expect.objectContaining({
...view,
schema: {
...basicTable.schema,
name: {
type: "string",
name: "name",
@ -261,6 +269,7 @@ describe("table sdk", () => {
id: {
type: "number",
name: "id",
order: undefined,
visible: true,
constraints: {
type: "number",
@ -270,7 +279,7 @@ describe("table sdk", () => {
type: "string",
name: "description",
order: 2,
visible: false,
visible: true,
width: 200,
constraints: {
type: "string",
@ -294,7 +303,6 @@ describe("table sdk", () => {
it("no table schema changes will not amend the view", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
}
const result = syncSchema(
_.cloneDeep(view),
@ -307,7 +315,6 @@ describe("table sdk", () => {
it("adding new columns will not change the view schema", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
}
const newTableSchema = {
@ -327,29 +334,26 @@ describe("table sdk", () => {
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
schemaUI: undefined,
schema: undefined,
})
})
it("deleting columns will not change the view schema", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
}
const { name, description, ...newTableSchema } = basicTable.schema
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
columns: ["id"],
schemaUI: undefined,
schema: undefined,
})
})
it("renaming mapped columns will update the view column mapping", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
}
const { description, ...newTableSchema } = {
...basicTable.schema,
@ -365,8 +369,7 @@ describe("table sdk", () => {
})
expect(result).toEqual({
...view,
columns: ["name", "id", "updatedDescription"],
schemaUI: undefined,
schema: undefined,
})
})
})
@ -375,8 +378,7 @@ describe("table sdk", () => {
it("no table schema changes will not amend the view", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },
@ -394,8 +396,7 @@ describe("table sdk", () => {
it("adding new columns will add them as not visible to the view", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },
@ -420,8 +421,8 @@ describe("table sdk", () => {
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
schemaUI: {
...view.schemaUI,
schema: {
...view.schema,
newField1: { visible: false },
newField2: { visible: false },
},
@ -431,8 +432,7 @@ describe("table sdk", () => {
it("deleting columns will remove them from the UI", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },
@ -444,9 +444,8 @@ describe("table sdk", () => {
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
columns: ["id"],
schemaUI: {
...view.schemaUI,
schema: {
...view.schema,
name: undefined,
description: undefined,
},
@ -456,8 +455,7 @@ describe("table sdk", () => {
it("can handle additions and deletions at the same them UI", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },
@ -476,9 +474,8 @@ describe("table sdk", () => {
const result = syncSchema(_.cloneDeep(view), newTableSchema, undefined)
expect(result).toEqual({
...view,
columns: ["id"],
schemaUI: {
...view.schemaUI,
schema: {
...view.schema,
name: undefined,
description: undefined,
newField1: { visible: false },
@ -489,8 +486,7 @@ describe("table sdk", () => {
it("renaming mapped columns will update the view column mapping and it's schema", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true },
id: { visible: true },
description: { visible: true, width: 150, icon: "ic-any" },
@ -511,9 +507,8 @@ describe("table sdk", () => {
})
expect(result).toEqual({
...view,
columns: ["name", "id", "updatedDescription"],
schemaUI: {
...view.schemaUI,
schema: {
...view.schema,
description: undefined,
updatedDescription: { visible: true, width: 150, icon: "ic-any" },
},
@ -523,8 +518,7 @@ describe("table sdk", () => {
it("changing no UI schema will not affect the view", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },
@ -548,8 +542,7 @@ describe("table sdk", () => {
it("changing table column UI fields will not affect the view schema", () => {
const view: ViewV2 = {
...basicView,
columns: ["name", "id", "description"],
schemaUI: {
schema: {
name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false },

View file

@ -5,11 +5,10 @@ export interface ViewResponse {
}
export interface CreateViewRequest
extends Omit<ViewV2, "version" | "id" | "columns" | "schemaUI"> {
extends Omit<ViewV2, "version" | "id" | "schema"> {
schema?: Record<string, UIFieldMetadata>
}
export interface UpdateViewRequest
extends Omit<ViewV2, "columns" | "schemaUI"> {
export interface UpdateViewRequest extends Omit<ViewV2, "schema"> {
schema?: Record<string, UIFieldMetadata>
}

View file

@ -25,8 +25,7 @@ export interface ViewV2 {
order?: SortOrder
type?: SortType
}
columns?: string[]
schemaUI?: Record<string, UIFieldMetadata>
schema?: Record<string, UIFieldMetadata>
}
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema