Skip to content

Commit

Permalink
Use faster query to fetch the latest aggregate (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkersner authored Nov 20, 2023
1 parent 4b84162 commit 985aa5f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions api/src/aggregate/aggregate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ export class AggregateService {
*/
async findLatest(latestAggregateDto: LatestAggregateDto) {
const { aggregatorHash } = latestAggregateDto
return await this.prisma.aggregate.findFirst({
where: { aggregator: { aggregatorHash } },
orderBy: [
{
timestamp: 'desc'
}
]
})
const query = Prisma.sql`SELECT aggregate_id as id, timestamp, value, aggregator_id as "aggregatorId"
FROM aggregates
WHERE aggregator_id = (SELECT aggregator_id FROM aggregators WHERE aggregator_hash = ${aggregatorHash})
ORDER BY timestamp DESC
LIMIT 1;`
const result: Prisma.AggregateScalarFieldEnum[] = await this.prisma.$queryRaw(query)
if (result.length == 1) {
return result[0]
} else {
throw Error(`Expected one row. Received ${result.length}`)
}
}
}

0 comments on commit 985aa5f

Please sign in to comment.