Enrich schema using the new data

This commit is contained in:
Adria Navarro 2023-07-31 17:15:51 +02:00
parent 878e09cfb0
commit da6136a108
2 changed files with 66 additions and 45 deletions

View File

@ -1,5 +1,11 @@
import { HTTPError, context } from "@budibase/backend-core" import { HTTPError, context } from "@budibase/backend-core"
import { TableSchema, UIFieldMetadata, View, ViewV2 } from "@budibase/types" import {
FieldSchema,
TableSchema,
UIFieldMetadata,
View,
ViewV2,
} from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "../../../db/utils" import * as utils from "../../../db/utils"
@ -73,37 +79,34 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) {
return view return view
} }
let schema = { ...tableSchema }
if (view.schemaUI) {
const viewOverridesEntries = Object.entries(view.schemaUI)
const viewSetsOrder = viewOverridesEntries.some(([_, v]) => v.order)
for (const [fieldName, schemaUI] of viewOverridesEntries) {
schema[fieldName] = {
...schema[fieldName],
...schemaUI,
order: viewSetsOrder
? schemaUI.order || undefined
: schema[fieldName].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
}
return { return {
...view, ...view,
schema: schema: schema,
!view?.columns || !Object.entries(view?.columns).length
? tableSchema
: enrichViewV2Schema(tableSchema, view.columns),
} }
} }
function enrichViewV2Schema(
tableSchema: TableSchema,
viewOverrides: Record<string, UIFieldMetadata>
) {
const result: TableSchema = {}
const viewOverridesEntries = Object.entries(viewOverrides)
const viewSetsOrder = viewOverridesEntries.some(([_, v]) => v.order)
for (const [columnName, columnUIMetadata] of viewOverridesEntries) {
if (!columnUIMetadata.visible) {
continue
}
if (!tableSchema[columnName]) {
continue
}
const tableFieldSchema = tableSchema[columnName]
if (viewSetsOrder) {
delete tableFieldSchema.order
}
result[columnName] = merge(tableFieldSchema, columnUIMetadata)
}
return result
}

View File

@ -102,18 +102,14 @@ describe("table sdk", () => {
}) })
}) })
it("if view schema only defines visiblility, should only fetch the selected fields", async () => { it("if view schema only defines columns, should only fetch the selected fields", async () => {
const tableId = basicTable._id! const tableId = basicTable._id!
const view: ViewV2 = { const view: ViewV2 = {
version: 2, version: 2,
id: generator.guid(), id: generator.guid(),
name: generator.guid(), name: generator.guid(),
tableId, tableId,
columns: { columns: ["name", "id"],
name: { visible: true },
id: { visible: true },
description: { visible: false },
},
} }
const res = enrichSchema(view, basicTable.schema) const res = enrichSchema(view, basicTable.schema)
@ -151,7 +147,7 @@ describe("table sdk", () => {
id: generator.guid(), id: generator.guid(),
name: generator.guid(), name: generator.guid(),
tableId, tableId,
columns: { unnexisting: { visible: true }, name: { visible: true } }, columns: ["unnexisting", "name"],
} }
const res = enrichSchema(view, basicTable.schema) const res = enrichSchema(view, basicTable.schema)
@ -175,16 +171,17 @@ describe("table sdk", () => {
) )
}) })
it("if view schema only defines visiblility, should only fetch the selected fields", async () => { it("if the view schema overrides the schema UI, the table schema should be overridden", async () => {
const tableId = basicTable._id! const tableId = basicTable._id!
const view: ViewV2 = { const view: ViewV2 = {
version: 2, version: 2,
id: generator.guid(), id: generator.guid(),
name: generator.guid(), name: generator.guid(),
tableId, tableId,
columns: { columns: ["name", "id", "description"],
name: { visible: true }, schemaUI: {
id: { visible: true }, name: { visible: true, width: 100 },
id: { visible: true, width: 20 },
description: { visible: false }, description: { visible: false },
}, },
} }
@ -200,7 +197,7 @@ describe("table sdk", () => {
name: "name", name: "name",
order: 2, order: 2,
visible: true, visible: true,
width: 80, width: 100,
constraints: { constraints: {
type: "string", type: "string",
}, },
@ -210,23 +207,34 @@ describe("table sdk", () => {
name: "id", name: "id",
order: 1, order: 1,
visible: true, visible: true,
width: 20,
constraints: { constraints: {
type: "number", type: "number",
}, },
}, },
description: {
type: "string",
name: "description",
visible: false,
width: 200,
constraints: {
type: "string",
},
},
}, },
}) })
) )
}) })
it("if view defines order, the table schema order should be ignored", async () => { it("if the view defines order, the table schema order should be ignored", async () => {
const tableId = basicTable._id! const tableId = basicTable._id!
const view: ViewV2 = { const view: ViewV2 = {
version: 2, version: 2,
id: generator.guid(), id: generator.guid(),
name: generator.guid(), name: generator.guid(),
tableId, tableId,
columns: { columns: ["name", "id", "description"],
schemaUI: {
name: { visible: true, order: 1 }, name: { visible: true, order: 1 },
id: { visible: true }, id: { visible: true },
description: { visible: false, order: 2 }, description: { visible: false, order: 2 },
@ -257,6 +265,16 @@ describe("table sdk", () => {
type: "number", type: "number",
}, },
}, },
description: {
type: "string",
name: "description",
order: 2,
visible: false,
width: 200,
constraints: {
type: "string",
},
},
}, },
}) })
) )