From 9ca5c684f46b4763f33e6558680bd5bcdc515998 Mon Sep 17 00:00:00 2001 From: Jonas Simoen Date: Tue, 16 Jan 2024 03:09:22 +0100 Subject: [PATCH] chore: use prisma Accelerate --- package.json | 5 +++-- src/controllers/Club.ts | 2 +- src/controllers/Match.ts | 6 ++++-- src/controllers/MatchEvent.ts | 3 ++- src/controllers/Page.ts | 6 ++++-- src/controllers/Player.ts | 1 + src/controllers/Statistic.ts | 4 +++- src/controllers/Team.ts | 24 ++++++++++++++++-------- src/controllers/User.ts | 9 ++++++--- src/controllers/UserAuth.ts | 3 ++- src/controllers/Week.ts | 24 ++++++++++++++++-------- src/db/client.ts | 5 +++-- src/utils/Common.ts | 6 ++++-- src/utils/DeserializeUser.ts | 1 + 14 files changed, 66 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 78a79f0..dd80433 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "@fastify/cors": "^8.2.1", "@fastify/static": "^6.10.2", "@netlify/functions": "^2.3.0", - "@prisma/client": "^5.6.0", + "@prisma/client": "^5.8.1", + "@prisma/extension-accelerate": "^0.6.2", "@types/express": "^4.17.20", "aws-lambda-fastify": "^2.2.0", "axios": "^1.4.0", @@ -27,7 +28,7 @@ "jsonwebtoken": "^9.0.0", "lodash": "^4.17.21", "module-alias": "^2.2.3", - "prisma": "^5.6.0", + "prisma": "^5.8.1", "qs": "^6.11.2", "serverless-http": "^3.2.0" }, diff --git a/src/controllers/Club.ts b/src/controllers/Club.ts index 99b593c..36226b7 100644 --- a/src/controllers/Club.ts +++ b/src/controllers/Club.ts @@ -3,7 +3,7 @@ import HttpError from "../utils/HttpError"; import axios from "axios"; export const GetClubsHandler = async (req: any, rep: any) => { - const clubs = await prisma.club.findMany({}); + const clubs = await prisma.club.findMany({cacheStrategy: { ttl: 60 }}); rep.send(clubs); } diff --git a/src/controllers/Match.ts b/src/controllers/Match.ts index 5183be8..7203bc7 100644 --- a/src/controllers/Match.ts +++ b/src/controllers/Match.ts @@ -10,7 +10,8 @@ export const GetMatchesHandler = async (req: any, rep: any) => { }, orderBy: { date: 'asc' - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(matches); } @@ -54,6 +55,7 @@ export const GetMatchHandler = async (req: any, rep: any) => { homeScore: true, awayScore: true, }, + cacheStrategy: { ttl: 60 }, }); rep.send(match); } @@ -104,7 +106,7 @@ export const ImportMatchesHandler = async (req: any, rep: any) => { throw new HttpError(Object.values(res.data.errors).reduce((s, v) => `${s}${v} `, '') as string, 429) } - const clubs = (await prisma.club.findMany()).map((club: any) => ({ + const clubs = (await prisma.club.findMany({cacheStrategy: { ttl: 60 }})).map((club: any) => ({ id: club.id, external: club.externalId, })); diff --git a/src/controllers/MatchEvent.ts b/src/controllers/MatchEvent.ts index f7e27fa..24fd35c 100644 --- a/src/controllers/MatchEvent.ts +++ b/src/controllers/MatchEvent.ts @@ -10,7 +10,8 @@ export const GetMatchEventsHandler = async (req: any, rep: any) => { }, include: { player: true - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(events); } diff --git a/src/controllers/Page.ts b/src/controllers/Page.ts index ae2416b..050b0c9 100644 --- a/src/controllers/Page.ts +++ b/src/controllers/Page.ts @@ -9,7 +9,8 @@ export const GetPagesHandler = async (req: any, rep: any) => { }, include: { translation: true - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(pages); @@ -23,7 +24,8 @@ export const GetPageHandler = async (req: any, rep: any) => { }, include: { translation: true - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(pages); diff --git a/src/controllers/Player.ts b/src/controllers/Player.ts index 9e1d6c6..c856295 100644 --- a/src/controllers/Player.ts +++ b/src/controllers/Player.ts @@ -8,6 +8,7 @@ import HttpError from "../utils/HttpError"; export const GetPlayersHandler = async (req: any, rep: any) => { const players = await prisma.player.findMany({ orderBy: [{ value: "desc" }, { clubId: "asc" }, { id: "asc" }], + cacheStrategy: { ttl: 60 }, }); rep.send(players); }; diff --git a/src/controllers/Statistic.ts b/src/controllers/Statistic.ts index 674abea..f7d4c9b 100644 --- a/src/controllers/Statistic.ts +++ b/src/controllers/Statistic.ts @@ -19,6 +19,7 @@ export const GetPlayerStatisticsHandler = async (req: any, rep: any) => { }, club: true }, + cacheStrategy: { ttl: 60 }, }); const allStatsPlayers = players.map((player: any) => { @@ -89,7 +90,8 @@ export const GetMatchStatisticsHandler = async (req: any, rep: any) => { const stats = await prisma.statistic.findMany({ where: { matchId: +req.params.matchId - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(stats); } diff --git a/src/controllers/Team.ts b/src/controllers/Team.ts index aeec033..1881882 100644 --- a/src/controllers/Team.ts +++ b/src/controllers/Team.ts @@ -18,7 +18,8 @@ export const PostAddTeamHandler = async (req: any, rep: any) => { select: { id: true, value: true, - } + }, + cacheStrategy: { ttl: 60 }, }); const weekId = await upcomingWeekId(); @@ -106,7 +107,8 @@ export const GetTeamHandler = async (req: any, rep: any) => { weekId: true, } } - } + }, + cacheStrategy: { ttl: 60 }, }); const players = playersWithMultipleSelections.map(({ selections, ...rest }) => ({ ...rest, @@ -119,14 +121,16 @@ export const GetTeamHandler = async (req: any, rep: any) => { }, include: { user: true - } + }, + cacheStrategy: { ttl: 60 }, }); const transfers = await prisma.transfer.findMany({ where: { teamId: +req.params.id, weekId, - } + }, + cacheStrategy: { ttl: 60 }, }) rep.send({ team: { ...team, freeHit: 0, bank: 0, tripleCaptain: 0, wildCard: 0 }, players, transfers: transfers }); } @@ -155,23 +159,27 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => { weekId: +req.params.weekId, } } - } + }, + cacheStrategy: { ttl: 60 }, }); const team = await prisma.team.findUnique({ where: { id: +req.params.id, - } + }, + cacheStrategy: { ttl: 60 }, }); const deadlineWeek = await prisma.week.findFirst({ where: { id: +req.params.weekId - } + }, + cacheStrategy: { ttl: 60 }, }); const transfers = await prisma.transfer.findMany({ where: { teamId: +req.params.id, weekId: +req.params.weekId, - } + }, + cacheStrategy: { ttl: 60 }, }); const weeklyData: [{ teamId: number, points: number, rank: number }] = await prisma.$queryRaw`SELECT "teamId", CAST(SUM(points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(points)) AS int) FROM "Selection" s WHERE "weekId" = ${+req.params.weekId} AND starting = 1 GROUP BY "teamId" ORDER BY rank DESC`; const globalData: [{ teamId: number, points: number, rank: number }] = await prisma.$queryRaw`SELECT "teamId", CAST(SUM(points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(points)) AS int) FROM "Selection" s WHERE starting = 1 GROUP BY "teamId" ORDER BY rank DESC`; diff --git a/src/controllers/User.ts b/src/controllers/User.ts index adf4419..c54edd4 100644 --- a/src/controllers/User.ts +++ b/src/controllers/User.ts @@ -4,7 +4,8 @@ export const GetProfileHandler = async (req: any, rep: any) => { const user = await prisma.user.findUnique({ where: { id: req.user.id - } + }, + cacheStrategy: { ttl: 60 }, }) rep.send(user); } @@ -29,14 +30,16 @@ export const GetTeamsHandler = async (req: any, rep: any) => { id: true, firstName: true, lastName: true, - } + }, + cacheStrategy: { ttl: 60 }, }); const teams = await prisma.team.findMany({ where: { user: { id: req.user.id } - } + }, + cacheStrategy: { ttl: 60 }, }) rep.send({teams, user}); } \ No newline at end of file diff --git a/src/controllers/UserAuth.ts b/src/controllers/UserAuth.ts index 9880f0d..4adf648 100644 --- a/src/controllers/UserAuth.ts +++ b/src/controllers/UserAuth.ts @@ -27,7 +27,8 @@ export const GoogleAuthHandler = async (req: AccessTokenRequest, rep: any) => { let user = await prisma.user.findUnique({ where: { email: googleUserInfo.email - } + }, + cacheStrategy: { ttl: 60 }, }); let firstSignIn = false; diff --git a/src/controllers/Week.ts b/src/controllers/Week.ts index 397a662..965ede0 100644 --- a/src/controllers/Week.ts +++ b/src/controllers/Week.ts @@ -5,7 +5,8 @@ export const GetWeeksHandler = async (req: any, rep: any) => { const weeks = await prisma.week.findMany({ orderBy: { id: 'asc' - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(weeks); } @@ -14,7 +15,8 @@ export const GetWeekHandler = async (req: any, rep: any) => { const week = await prisma.week.findUnique({ where: { id: +req.params.id - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send(week); } @@ -53,7 +55,8 @@ export const PostWeekValidateHandler = async (req: any, rep: any) => { by: ['playerId'], _sum: { points: true, - } + }, + cacheStrategy: { ttl: 60 }, }); const teamPoints = await prisma.selection.groupBy({ @@ -64,7 +67,8 @@ export const PostWeekValidateHandler = async (req: any, rep: any) => { by: ['teamId'], _sum: { points: true - } + }, + cacheStrategy: { ttl: 60 }, }); const teamsWithSelections = await prisma.team.findMany({ @@ -82,7 +86,8 @@ export const PostWeekValidateHandler = async (req: any, rep: any) => { } } } - } + }, + cacheStrategy: { ttl: 60 }, }); try { await prisma.$transaction(async (prisma) => { @@ -188,7 +193,8 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => { const weeks = await prisma.week.findMany({ orderBy: { id: 'asc' - } + }, + cacheStrategy: { ttl: 60 }, }); const deadlineWeek = await prisma.week.findFirst({ where: { @@ -198,7 +204,8 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => { }, orderBy: { deadlineDate: 'asc', - } + }, + cacheStrategy: { ttl: 60 }, }); const displayWeek = await prisma.week.findFirst({ where: { @@ -208,7 +215,8 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => { }, orderBy: { deadlineDate: 'desc' - } + }, + cacheStrategy: { ttl: 60 }, }); rep.send({ deadlineInfo: { diff --git a/src/db/client.ts b/src/db/client.ts index 89e2241..3c195bb 100644 --- a/src/db/client.ts +++ b/src/db/client.ts @@ -1,4 +1,5 @@ -import { PrismaClient } from "@prisma/client"; +import { PrismaClient } from "@prisma/client/edge"; +import { withAccelerate } from "@prisma/extension-accelerate"; export const prisma = new PrismaClient({ // log: ['query', 'info', 'warn', 'error'], -}); +}).$extends(withAccelerate()); diff --git a/src/utils/Common.ts b/src/utils/Common.ts index 5523618..b117ad7 100644 --- a/src/utils/Common.ts +++ b/src/utils/Common.ts @@ -9,7 +9,8 @@ export const upcomingWeekId = async () => { }, orderBy: { deadlineDate: 'asc', - } + }, + cacheStrategy: { ttl: 60 }, }); return (week && week.id) || 0; } @@ -19,7 +20,8 @@ export const finalWeekId = async () => { orderBy: { deadlineDate: 'desc' }, - take: 1 + take: 1, + cacheStrategy: { ttl: 60 }, }); return (week && week.id) || 0; diff --git a/src/utils/DeserializeUser.ts b/src/utils/DeserializeUser.ts index 1e3da46..881ad43 100644 --- a/src/utils/DeserializeUser.ts +++ b/src/utils/DeserializeUser.ts @@ -12,6 +12,7 @@ export const reIssueAccessToken = async ({ refreshToken }: { refreshToken: strin where: { id: get(decoded, "id"), }, + cacheStrategy: { ttl: 60 }, }); if (!user ) return false;