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 tableId = ctx.params.tableId
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
const enrichedSchema = await sdk.tables.enrichRelationshipSchema(table)
const result = sdk.tables.enrichViewSchemas({ const result = sdk.tables.enrichViewSchemas({
...table, ...table,
schema: enrichedSchema, schema: await sdk.tables.enrichRelationshipSchema(table.schema),
}) })
ctx.body = result ctx.body = result
} }

View File

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

View File

@ -33,7 +33,7 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
if (!found) { if (!found) {
throw new Error("No view found") throw new Error("No view found")
} }
return enrichSchema(found, table.schema) return await enrichSchema(found, table.schema)
} }
export async function create( 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, view: ViewV2,
tableSchema: TableSchema tableSchema: TableSchema
): ViewV2Enriched { ): Promise<ViewV2Enriched> {
let schema = cloneDeep(tableSchema) let schema = cloneDeep(tableSchema)
const anyViewOrder = Object.values(view.schema || {}).some( const anyViewOrder = Object.values(view.schema || {}).some(
ui => ui.order != null ui => ui.order != null
) )
@ -171,6 +172,8 @@ export function enrichSchema(
} }
} }
schema = await sdk.tables.enrichRelationshipSchema(schema)
return { return {
...view, ...view,
schema: schema, schema: schema,

View File

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

View File

@ -3,6 +3,7 @@ import {
FieldSchema, FieldSchema,
FieldType, FieldType,
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
RelationshipType,
Table, Table,
TableSchema, TableSchema,
TableSourceType, TableSourceType,
@ -10,6 +11,7 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import { enrichSchema, syncSchema } from ".." import { enrichSchema, syncSchema } from ".."
import sdk from "../../../../sdk"
describe("table sdk", () => { describe("table sdk", () => {
const basicTable: Table = { const basicTable: Table = {
@ -68,7 +70,7 @@ describe("table sdk", () => {
tableId, tableId,
} }
const res = enrichSchema(view, basicTable.schema) const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual({ expect(res).toEqual({
...view, ...view,
@ -126,7 +128,7 @@ describe("table sdk", () => {
}, },
} }
const res = enrichSchema(view, basicTable.schema) const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual({ expect(res).toEqual({
...view, ...view,
@ -164,7 +166,7 @@ describe("table sdk", () => {
}, },
} }
const res = enrichSchema(view, basicTable.schema) const res = await enrichSchema(view, basicTable.schema)
expect(res).toEqual({ expect(res).toEqual({
...view, ...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(res).toEqual(
expect.objectContaining({ 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(res).toEqual(
expect.objectContaining({ 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", () => { describe("syncSchema", () => {