Skip to content

Commit

Permalink
chore: use prisma Accelerate
Browse files Browse the repository at this point in the history
  • Loading branch information
jonassimoen committed Jan 16, 2024
1 parent 8e6d658 commit 9ca5c68
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 33 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Club.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions src/controllers/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const GetMatchesHandler = async (req: any, rep: any) => {
},
orderBy: {
date: 'asc'
}
},
cacheStrategy: { ttl: 60 },
});
rep.send(matches);
}
Expand Down Expand Up @@ -54,6 +55,7 @@ export const GetMatchHandler = async (req: any, rep: any) => {
homeScore: true,
awayScore: true,
},
cacheStrategy: { ttl: 60 },
});
rep.send(match);
}
Expand Down Expand Up @@ -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,
}));
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/MatchEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const GetMatchEventsHandler = async (req: any, rep: any) => {
},
include: {
player: true
}
},
cacheStrategy: { ttl: 60 },
});
rep.send(events);
}
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const GetPagesHandler = async (req: any, rep: any) => {
},
include: {
translation: true
}
},
cacheStrategy: { ttl: 60 },
});

rep.send(pages);
Expand All @@ -23,7 +24,8 @@ export const GetPageHandler = async (req: any, rep: any) => {
},
include: {
translation: true
}
},
cacheStrategy: { ttl: 60 },
});

rep.send(pages);
Expand Down
1 change: 1 addition & 0 deletions src/controllers/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/Statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const GetPlayerStatisticsHandler = async (req: any, rep: any) => {
},
club: true
},
cacheStrategy: { ttl: 60 },
});

const allStatsPlayers = players.map((player: any) => {
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 16 additions & 8 deletions src/controllers/Team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const PostAddTeamHandler = async (req: any, rep: any) => {
select: {
id: true,
value: true,
}
},
cacheStrategy: { ttl: 60 },
});

const weekId = await upcomingWeekId();
Expand Down Expand Up @@ -106,7 +107,8 @@ export const GetTeamHandler = async (req: any, rep: any) => {
weekId: true,
}
}
}
},
cacheStrategy: { ttl: 60 },
});
const players = playersWithMultipleSelections.map(({ selections, ...rest }) => ({
...rest,
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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`;
Expand Down
9 changes: 6 additions & 3 deletions src/controllers/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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});
}
3 changes: 2 additions & 1 deletion src/controllers/UserAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 16 additions & 8 deletions src/controllers/Week.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -82,7 +86,8 @@ export const PostWeekValidateHandler = async (req: any, rep: any) => {
}
}
}
}
},
cacheStrategy: { ttl: 60 },
});
try {
await prisma.$transaction(async (prisma) => {
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -208,7 +215,8 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => {
},
orderBy: {
deadlineDate: 'desc'
}
},
cacheStrategy: { ttl: 60 },
});
rep.send({
deadlineInfo: {
Expand Down
5 changes: 3 additions & 2 deletions src/db/client.ts
Original file line number Diff line number Diff line change
@@ -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());
6 changes: 4 additions & 2 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const upcomingWeekId = async () => {
},
orderBy: {
deadlineDate: 'asc',
}
},
cacheStrategy: { ttl: 60 },
});
return (week && week.id) || 0;
}
Expand All @@ -19,7 +20,8 @@ export const finalWeekId = async () => {
orderBy: {
deadlineDate: 'desc'
},
take: 1
take: 1,
cacheStrategy: { ttl: 60 },
});

return (week && week.id) || 0;
Expand Down
1 change: 1 addition & 0 deletions src/utils/DeserializeUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const reIssueAccessToken = async ({ refreshToken }: { refreshToken: strin
where: {
id: get(decoded, "id"),
},
cacheStrategy: { ttl: 60 },
});

if (!user ) return false;
Expand Down

0 comments on commit 9ca5c68

Please sign in to comment.