diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 22c4e98524..be49d66c82 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -91,10 +91,9 @@ export async function find(ctx: UserCtx) { 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 } diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 39e43b38bc..5a505fd5b4 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -146,7 +146,7 @@ export async function getTables(tableIds: string[]): Promise { } export async function enrichRelationshipSchema( - table: Table + schema: TableSchema ): Promise { const tableCache: Record = {} @@ -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) } diff --git a/packages/server/src/sdk/app/views/external.ts b/packages/server/src/sdk/app/views/external.ts index 2b3e271597..3afd7e9bf9 100644 --- a/packages/server/src/sdk/app/views/external.ts +++ b/packages/server/src/sdk/app/views/external.ts @@ -33,7 +33,7 @@ export async function getEnriched(viewId: string): Promise { if (!found) { throw new Error("No view found") } - return enrichSchema(found, table.schema) + return await enrichSchema(found, table.schema) } export async function create( diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index bed6254943..3e33186797 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -153,11 +153,12 @@ export function allowedFields(view: View | ViewV2) { ] } -export function enrichSchema( +export async function enrichSchema( view: ViewV2, tableSchema: TableSchema -): ViewV2Enriched { +): Promise { 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, diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index 7b2f9f6c80..19a9f6ab14 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -24,7 +24,7 @@ export async function getEnriched(viewId: string): Promise { if (!found) { throw new Error("No view found") } - return enrichSchema(found, table.schema) + return await enrichSchema(found, table.schema) } export async function create( diff --git a/packages/server/src/sdk/app/views/tests/views.spec.ts b/packages/server/src/sdk/app/views/tests/views.spec.ts index 508285651a..265e6b159f 100644 --- a/packages/server/src/sdk/app/views/tests/views.spec.ts +++ b/packages/server/src/sdk/app/views/tests/views.spec.ts @@ -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", () => {