refactor: rewrite countAll SQL to use GROUP BY count

Signed-off-by: izzy <me@insrt.uk>
This commit is contained in:
izzy
2026-02-06 17:20:27 +00:00
parent 3d5db67656
commit 08fb19b5b7

View File

@@ -39,28 +39,16 @@ export class IntegrityRepository {
}
@GenerateSql({ params: [] })
getIntegrityReportSummary() {
return this.db
async getIntegrityReportSummary() {
const counts = await this.db
.selectFrom('integrity_report')
.select((eb) =>
eb.fn
.countAll<number>()
.filterWhere('type', '=', IntegrityReportType.ChecksumFail)
.as(IntegrityReportType.ChecksumFail),
)
.select((eb) =>
eb.fn
.countAll<number>()
.filterWhere('type', '=', IntegrityReportType.MissingFile)
.as(IntegrityReportType.MissingFile),
)
.select((eb) =>
eb.fn
.countAll<number>()
.filterWhere('type', '=', IntegrityReportType.UntrackedFile)
.as(IntegrityReportType.UntrackedFile),
)
.executeTakeFirstOrThrow();
.select(['type', this.db.fn.countAll<number>().as('count')])
.groupBy('type')
.execute();
return Object.fromEntries(
Object.values(IntegrityReportType).map((type) => [type, counts.find((count) => count.type === type)?.count || 0]),
) as Record<IntegrityReportType, number>;
}
@GenerateSql({ params: [{ cursor: DummyValue.NUMBER, limit: 100 }, DummyValue.STRING] })