Enrich view schemas

This commit is contained in:
Adria Navarro 2024-08-22 12:05:24 +02:00
parent 906abbb12a
commit 55d4e2dffe
6 changed files with 96 additions and 14 deletions

View File

@ -91,10 +91,9 @@ export async function find(ctx: UserCtx<void, TableResponse>) {
const tableId = ctx.params.tableId
const table = await sdk.tables.getTable(tableId)
const enrichedSchema = await sdk.tables.enrichRelationshipSchema(table)
const result = sdk.tables.enrichViewSchemas({
...table,
schema: enrichedSchema,
schema: await sdk.tables.enrichRelationshipSchema(table.schema),
})
ctx.body = result
}

View File

@ -146,7 +146,7 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
}
export async function enrichRelationshipSchema(
table: Table
schema: TableSchema
): Promise<TableSchema> {
const tableCache: Record<string, Table> = {}
@ -178,8 +178,8 @@ export async function enrichRelationshipSchema(
}
const result: TableSchema = {}
for (const fieldName of Object.keys(table.schema)) {
const field = { ...table.schema[fieldName] }
for (const fieldName of Object.keys(schema)) {
const field = { ...schema[fieldName] }
if (field.type === FieldType.LINK) {
await populateRelTableSchema(field)
}

View File

@ -33,7 +33,7 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
if (!found) {
throw new Error("No view found")
}
return enrichSchema(found, table.schema)
return await enrichSchema(found, table.schema)
}
export async function create(

View File

@ -153,11 +153,12 @@ export function allowedFields(view: View | ViewV2) {
]
}
export function enrichSchema(
export async function enrichSchema(
view: ViewV2,
tableSchema: TableSchema
): ViewV2Enriched {
): Promise<ViewV2Enriched> {
let schema = cloneDeep(tableSchema)
const anyViewOrder = Object.values(view.schema || {}).some(
ui => ui.order != null
)
@ -171,6 +172,8 @@ export function enrichSchema(
}
}
schema = await sdk.tables.enrichRelationshipSchema(schema)
return {
...view,
schema: schema,

View File

@ -24,7 +24,7 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
if (!found) {
throw new Error("No view found")
}
return enrichSchema(found, table.schema)
return await enrichSchema(found, table.schema)
}
export async function create(

View File

@ -3,6 +3,7 @@ import {
FieldSchema,
FieldType,
INTERNAL_TABLE_SOURCE_ID,
RelationshipType,
Table,
TableSchema,
TableSourceType,
@ -10,6 +11,7 @@ import {
} from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
import { enrichSchema, syncSchema } from ".."
import sdk from "../../../../sdk"
describe("table sdk", () => {
const basicTable: Table = {
@ -68,7 +70,7 @@ describe("table sdk", () => {
tableId,
}
const res = enrichSchema(view, basicTable.schema)
const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual({
...view,
@ -126,7 +128,7 @@ describe("table sdk", () => {
},
}
const res = enrichSchema(view, basicTable.schema)
const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual({
...view,
@ -164,7 +166,7 @@ describe("table sdk", () => {
},
}
const res = enrichSchema(view, basicTable.schema)
const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual({
...view,
@ -203,7 +205,7 @@ describe("table sdk", () => {
},
}
const res = enrichSchema(view, basicTable.schema)
const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual(
expect.objectContaining({
@ -258,7 +260,7 @@ describe("table sdk", () => {
},
}
const res = enrichSchema(view, basicTable.schema)
const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual(
expect.objectContaining({
@ -298,6 +300,84 @@ describe("table sdk", () => {
})
)
})
it("should include related fields", async () => {
const table: Table = {
...basicTable,
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
other: {
name: "other",
type: FieldType.LINK,
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: "table",
tableId: "otherTableId",
},
},
}
const otherTable: Table = {
...basicTable,
primaryDisplay: "title",
schema: {
title: {
name: "title",
type: FieldType.STRING,
},
age: {
name: "age",
type: FieldType.NUMBER,
},
},
}
const tableId = table._id!
const getTableSpy = jest.spyOn(sdk.tables, "getTable")
getTableSpy.mockResolvedValueOnce(otherTable)
const view: ViewV2 = {
version: 2,
id: generator.guid(),
name: generator.guid(),
tableId,
schema: {
name: { visible: true },
other: { visible: true },
},
}
const res = await enrichSchema(view, table.schema)
expect(res).toEqual(
expect.objectContaining({
...view,
schema: {
name: {
...table.schema.name,
visible: true,
},
other: {
...table.schema.other,
visible: true,
schema: {
title: {
visible: true,
readonly: true,
},
age: {
visible: false,
readonly: false,
},
},
},
},
})
)
})
})
describe("syncSchema", () => {