From 5715f7e9c15776d5c1bf36b61556d3591e15d78d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 22 Aug 2024 12:10:52 +0200 Subject: [PATCH] Fix promises --- .../server/src/api/controllers/table/index.ts | 10 ++++++--- packages/server/src/sdk/app/tables/getters.ts | 21 +++++++++++-------- .../src/sdk/app/tables/tests/tables.spec.ts | 2 +- .../src/sdk/app/views/tests/views.spec.ts | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index be49d66c82..9744516c4a 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -84,14 +84,18 @@ export async function fetch(ctx: UserCtx) { } }) - ctx.body = [...internal, ...external].map(sdk.tables.enrichViewSchemas) + const result: FetchTablesResponse = [] + for (const table of [...internal, ...external]) { + result.push(await sdk.tables.enrichViewSchemas(table)) + } + ctx.body = result // } export async function find(ctx: UserCtx) { const tableId = ctx.params.tableId const table = await sdk.tables.getTable(tableId) - const result = sdk.tables.enrichViewSchemas({ + const result = await sdk.tables.enrichViewSchemas({ ...table, schema: await sdk.tables.enrichRelationshipSchema(table.schema), }) @@ -109,7 +113,7 @@ export async function save(ctx: UserCtx) { const api = pickApi({ table }) let savedTable = await api.save(ctx, renaming) if (!table._id) { - savedTable = sdk.tables.enrichViewSchemas(savedTable) + savedTable = await sdk.tables.enrichViewSchemas(savedTable) await events.table.created(savedTable) } else { await events.table.updated(savedTable) diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 5a505fd5b4..aeab32b328 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -189,16 +189,19 @@ export async function enrichRelationshipSchema( return result } -export function enrichViewSchemas(table: Table): TableResponse { +export async function enrichViewSchemas(table: Table): Promise { + const views = [] + for (const view of Object.values(table.views ?? [])) { + if (sdk.views.isV2(view)) { + views.push(await sdk.views.enrichSchema(view, table.schema)) + } else views.push(view) + } + return { ...table, - views: Object.values(table.views ?? []) - .map(v => - sdk.views.isV2(v) ? sdk.views.enrichSchema(v, table.schema) : v - ) - .reduce((p, v) => { - p[v.name!] = v - return p - }, {} as TableViewsResponse), + views: views.reduce((p, v) => { + p[v.name!] = v + return p + }, {} as TableViewsResponse), } } diff --git a/packages/server/src/sdk/app/tables/tests/tables.spec.ts b/packages/server/src/sdk/app/tables/tests/tables.spec.ts index 6e2cf9efa8..41ac808f5c 100644 --- a/packages/server/src/sdk/app/tables/tests/tables.spec.ts +++ b/packages/server/src/sdk/app/tables/tests/tables.spec.ts @@ -75,7 +75,7 @@ describe("table sdk", () => { const view1 = getTable() const view2 = getTable() const view3 = getTable() - const res = sdk.tables.enrichViewSchemas({ + const res = await sdk.tables.enrichViewSchemas({ ...basicTable, views: { [view1.name]: view1, 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 265e6b159f..bfdd3e9c2a 100644 --- a/packages/server/src/sdk/app/views/tests/views.spec.ts +++ b/packages/server/src/sdk/app/views/tests/views.spec.ts @@ -60,7 +60,7 @@ describe("table sdk", () => { }, } - describe("enrichViewSchemas", () => { + describe("enrichSchema", () => { it("should fetch the default schema if not overridden", async () => { const tableId = basicTable._id! const view: ViewV2 = {