Fix promises

This commit is contained in:
Adria Navarro 2024-08-22 12:10:52 +02:00
parent 55d4e2dffe
commit 5715f7e9c1
4 changed files with 21 additions and 14 deletions

View File

@ -84,14 +84,18 @@ export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
} }
}) })
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<void, TableResponse>) { 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 result = sdk.tables.enrichViewSchemas({ const result = await sdk.tables.enrichViewSchemas({
...table, ...table,
schema: await sdk.tables.enrichRelationshipSchema(table.schema), schema: await sdk.tables.enrichRelationshipSchema(table.schema),
}) })
@ -109,7 +113,7 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const api = pickApi({ table }) const api = pickApi({ table })
let savedTable = await api.save(ctx, renaming) let savedTable = await api.save(ctx, renaming)
if (!table._id) { if (!table._id) {
savedTable = sdk.tables.enrichViewSchemas(savedTable) savedTable = await sdk.tables.enrichViewSchemas(savedTable)
await events.table.created(savedTable) await events.table.created(savedTable)
} else { } else {
await events.table.updated(savedTable) await events.table.updated(savedTable)

View File

@ -189,16 +189,19 @@ export async function enrichRelationshipSchema(
return result return result
} }
export function enrichViewSchemas(table: Table): TableResponse { export async function enrichViewSchemas(table: Table): Promise<TableResponse> {
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 { return {
...table, ...table,
views: Object.values(table.views ?? []) views: views.reduce((p, v) => {
.map(v => p[v.name!] = v
sdk.views.isV2(v) ? sdk.views.enrichSchema(v, table.schema) : v return p
) }, {} as TableViewsResponse),
.reduce((p, v) => {
p[v.name!] = v
return p
}, {} as TableViewsResponse),
} }
} }

View File

@ -75,7 +75,7 @@ describe("table sdk", () => {
const view1 = getTable() const view1 = getTable()
const view2 = getTable() const view2 = getTable()
const view3 = getTable() const view3 = getTable()
const res = sdk.tables.enrichViewSchemas({ const res = await sdk.tables.enrichViewSchemas({
...basicTable, ...basicTable,
views: { views: {
[view1.name]: view1, [view1.name]: view1,

View File

@ -60,7 +60,7 @@ describe("table sdk", () => {
}, },
} }
describe("enrichViewSchemas", () => { describe("enrichSchema", () => {
it("should fetch the default schema if not overridden", async () => { it("should fetch the default schema if not overridden", async () => {
const tableId = basicTable._id! const tableId = basicTable._id!
const view: ViewV2 = { const view: ViewV2 = {