Skip to content

Commit 68b2565

Browse files
committed
refactor use query to agg pipe count
1 parent 24b17b0 commit 68b2565

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/backend/src/graphql/queries/tiles/get-table-connections.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import getTables from './get-tables'
66

77
interface ExtendedFlow extends Flow {
88
tableid: string
9+
count: number
910
}
1011

1112
interface TableConnection {
@@ -29,16 +30,18 @@ const getTableConnections: QueryResolvers['getTableConnections'] = async (
2930
// get distinct rows of tables used in flows
3031
// returns flow id and table id
3132
const distinctTableFlows = await Flow.query()
32-
.distinct('flows.id', Flow.raw("steps.parameters ->> 'tableId' AS tableId"))
33+
.select(Flow.raw("steps.parameters ->> 'tableId' AS tableid"))
34+
.countDistinct('flows.id')
3335
.innerJoinRelated('steps')
3436
.innerJoinRelated('user')
3537
.where('steps.app_key', 'tiles')
3638
.whereRaw("steps.parameters ->> 'tableId' IS NOT NULL")
3739
.whereRaw(`steps.parameters ->> 'tableId' IN (${tableIdStr})`)
40+
.groupByRaw("steps.parameters ->> 'tableId'")
3841

3942
const result = distinctTableFlows.reduce(
40-
(acc: TableConnection, obj: ExtendedFlow) => {
41-
acc[obj.tableid] = (acc[obj.tableid] || 0) + 1
43+
(acc: TableConnection, row: ExtendedFlow) => {
44+
acc[row.tableid] = row.count
4245
return acc
4346
},
4447
{},

0 commit comments

Comments
 (0)