From fdb5fb57cec46a2fd41016ddc8d40a1ff62c81ba Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Thu, 8 Jun 2023 12:13:30 +0800 Subject: [PATCH] group columns ahead of time --- studio/stores/pgmeta/ForeignTableStore.ts | 8 +++++++- studio/stores/pgmeta/MaterializedViewStore.ts | 8 +++++++- studio/stores/pgmeta/TableStore.ts | 9 +++++++-- studio/stores/pgmeta/ViewStore.ts | 8 +++++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/studio/stores/pgmeta/ForeignTableStore.ts b/studio/stores/pgmeta/ForeignTableStore.ts index b216e7131b1..84710e16e7a 100644 --- a/studio/stores/pgmeta/ForeignTableStore.ts +++ b/studio/stores/pgmeta/ForeignTableStore.ts @@ -37,9 +37,15 @@ export default class ForeignTableStore extends PostgresMetaInterface { + acc[curr.table_id] ??= [] + acc[curr.table_id].push(curr) + return acc + }, {} as Record) const foreignTables: PostgresTable[] = [] foreignTablesResponse.forEach((foreignTable: PostgresTable) => { - const columns = columnsResponse.filter((x: PostgresColumn) => x.table_id === foreignTable.id) + const columns = columnsByTableId[foreignTable.id] foreignTables.push({ ...foreignTable, columns }) }) diff --git a/studio/stores/pgmeta/MaterializedViewStore.ts b/studio/stores/pgmeta/MaterializedViewStore.ts index 9b2d0ed77c2..71a98dc43df 100644 --- a/studio/stores/pgmeta/MaterializedViewStore.ts +++ b/studio/stores/pgmeta/MaterializedViewStore.ts @@ -39,9 +39,15 @@ export default class MaterializedViewStore extends PostgresMetaInterface { + acc[curr.table_id] ??= [] + acc[curr.table_id].push(curr) + return acc + }, {} as Record) const materializedViews: PostgresMaterializedView[] = [] materializedViewsResponse.forEach((materializedView: PostgresMaterializedView) => { - const columns = columnsResponse.filter((x: PostgresColumn) => x.table_id === materializedView.id) + const columns = columnsByTableId[materializedView.id] materializedViews.push({ ...materializedView, columns }) }) diff --git a/studio/stores/pgmeta/TableStore.ts b/studio/stores/pgmeta/TableStore.ts index 7571e848bd7..f6da9e1807d 100644 --- a/studio/stores/pgmeta/TableStore.ts +++ b/studio/stores/pgmeta/TableStore.ts @@ -42,10 +42,15 @@ export default class TableStore extends PostgresMetaInterface { if (columnsResponse.error) throw columnsResponse.error // merge 2 response to create the final array + const columnsByTableId = (columnsResponse as PostgresColumn[]) + .reduce((acc, curr) => { + acc[curr.table_id] ??= [] + acc[curr.table_id].push(curr) + return acc + }, {} as Record) const tables: PostgresTable[] = [] tablesResponse.forEach((table: PostgresTable) => { - const tableId = table.id - const columns = columnsResponse.filter((x: PostgresColumn) => x.table_id === tableId) + const columns = columnsByTableId[table.id] tables.push({ ...table, columns }) }) diff --git a/studio/stores/pgmeta/ViewStore.ts b/studio/stores/pgmeta/ViewStore.ts index ae83f4482c4..063530b2390 100644 --- a/studio/stores/pgmeta/ViewStore.ts +++ b/studio/stores/pgmeta/ViewStore.ts @@ -37,9 +37,15 @@ export default class ViewStore extends PostgresMetaInterface { if (columnsResponse.error) throw columnsResponse.error // merge 2 response to create the final array + const columnsByTableId = (columnsResponse as PostgresColumn[]) + .reduce((acc, curr) => { + acc[curr.table_id] ??= [] + acc[curr.table_id].push(curr) + return acc + }, {} as Record) const views: PostgresView[] = [] viewsResponse.forEach((view: PostgresView) => { - const columns = columnsResponse.filter((x: PostgresColumn) => x.table_id === view.id) + const columns = columnsByTableId[view.id] views.push({ ...view, columns }) })