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 table = await sdk.tables.getTable(view?.tableId)
const viewFields = const viewFields =
(view.columns && (view.schema &&
Object.entries(view.columns).length && Object.entries(view.schema).length &&
Object.keys(sdk.views.enrichSchema(view, table.schema).schema)) || Object.keys(sdk.views.enrichSchema(view, table.schema).schema)) ||
undefined undefined

View File

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

View File

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

View File

@ -42,7 +42,7 @@ export async function trimViewFields<T extends Row>(
data: T data: T
): Promise<T> { ): Promise<T> {
const view = await sdk.views.get(viewId) 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 return data
} }

View File

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

View File

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

View File

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

View File

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