api: improve get events sql and setup simple caching

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-12-12 15:54:10 +01:00
parent f82636a0d9
commit 3bd06b610e
4 changed files with 44 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
import { createTRPCRouter, protectedProcedure } from '@/server/api/trpc';
import * as cache from '@/server/cache';
import { db } from '@/server/db';
import { getUniqueEvents } from '@/server/services/event.service';
import { getProjectBySlug } from '@/server/services/project.service';
import type {
IChartEvent,
@@ -27,15 +28,8 @@ export const chartRouter = createTRPCRouter({
const project = await getProjectBySlug(projectSlug);
const events = await cache.getOr(
`events_${project.id}`,
1000 * 60 * 60,
() =>
db.event.findMany({
take: 500,
distinct: ['name'],
where: {
project_id: project.id,
},
})
1000 * 60 * 60 * 24,
() => getUniqueEvents({ projectId: project.id })
);
return events;