Skip to content

Commit c1c0234

Browse files
committed
fix: remove unnecessary data source refetch
1 parent 0979712 commit c1c0234

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/server/trpc/routers/dataSource.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,20 @@ export const dataSourceRouter = router({
6565
.leftJoin("dataRecord", "dataRecord.dataSourceId", "dataSource.id")
6666
.where("organisationId", "=", ctx.organisation.id)
6767
.selectAll("dataSource")
68-
.select(db.fn.count("dataRecord.id").as("recordCount"))
68+
.select(db.fn.count("dataRecord.id").distinct().as("recordCount"))
6969
.groupBy("dataSource.id")
7070
.execute();
7171

7272
return addImportInfo(dataSources);
7373
}),
7474
byId: dataSourceOwnerProcedure.query(async ({ ctx }) => {
75-
const dataSource = await db
76-
.selectFrom("dataSource")
77-
.leftJoin("dataRecord", "dataRecord.dataSourceId", "dataSource.id")
78-
.where("organisationId", "=", ctx.organisation.id)
79-
.where("dataSource.id", "=", ctx.dataSource.id)
80-
.selectAll("dataSource")
81-
.select(db.fn.count("dataRecord.id").as("recordCount"))
82-
.groupBy("dataSource.id")
75+
const recordCount = await db
76+
.selectFrom("dataRecord")
77+
.where("dataSourceId", "=", ctx.dataSource.id)
78+
.select(db.fn.countAll().as("count"))
8379
.executeTakeFirst();
8480

85-
if (!dataSource) {
86-
throw new TRPCError({
87-
code: "NOT_FOUND",
88-
message: "Data source not found",
89-
});
90-
}
91-
92-
const dataSourceIds = dataSource.enrichments
81+
const dataSourceIds = ctx.dataSource.enrichments
9382
.filter((e) => e.sourceType === EnrichmentSourceType.DataSource)
9483
.map((e) => e.dataSourceId)
9584
.filter((id) => typeof id === "string");
@@ -103,15 +92,15 @@ export const dataSourceRouter = router({
10392
),
10493
]);
10594
return {
106-
...dataSource,
95+
...ctx.dataSource,
10796
config: {
108-
...dataSource.config,
97+
...ctx.dataSource.config,
10998
__SERIALIZE_CREDENTIALS: true,
11099
},
111100
enrichmentInfo,
112101
importInfo,
113102
enrichmentDataSources,
114-
recordCount: Number(dataSource.recordCount) || 0,
103+
recordCount: Number(recordCount?.count) || 0,
115104
};
116105
}),
117106

0 commit comments

Comments
 (0)