Skip to content

Commit

Permalink
chore: add caching to sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jonassimoen committed Apr 12, 2024
1 parent f09ce76 commit 2936244
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dockerfile
.dockerignore
node_modules
.git
2 changes: 1 addition & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ server.get("/ping", (req: any, res: any) => {
res.status(200).send();
});
server.get("/metrics/prisma", async (req: any, res: any) => {
res.send(await prisma.$metrics.prometheus());
res.send(/*await prisma.$metrics.prometheus()*/);
})

server.setErrorHandler((err, req, rep) => {
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

generator client {
provider = "prisma-client-js"
previewFeatures = ["metrics"]
// previewFeatures = ["metrics"]
}

datasource db {
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import axios from "axios";

export const GetMatchesHandler = async (req: any, rep: any) => {
const matches = await prisma.match.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
include: {
home: true,
away: true
Expand All @@ -17,6 +21,10 @@ export const GetMatchesHandler = async (req: any, rep: any) => {

export const GetMatchHandler = async (req: any, rep: any) => {
const match = await prisma.match.findUnique({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: +req.params.id
},
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import HttpError from "../utils/HttpError";

export const GetPlayersHandler = async (req: any, rep: any) => {
const players = await prisma.player.findMany({
cacheStrategy: {
ttl: 300,
swr: 600,
},
orderBy: [{ value: "desc" }, { clubId: "asc" }, { id: "asc" }],
});
rep.send(players);
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/Statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { pick } from "lodash";

export const GetPlayerStatisticsHandler = async (req: any, rep: any) => {
const players = await prisma.player.findMany({
cacheStrategy: {
ttl: 300,
swr: 120,
},
include: {
stats: {
where: {
Expand Down Expand Up @@ -88,6 +92,10 @@ export const GetPlayerStatisticsHandler = async (req: any, rep: any) => {

export const GetMatchStatisticsHandler = async (req: any, rep: any) => {
const stats = await prisma.statistic.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
matchId: +req.params.matchId
}
Expand Down
28 changes: 28 additions & 0 deletions src/controllers/Team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export const DeleteDropTeamHandler = async (req: any, rep: any) => {
export const GetTeamHandler = async (req: any, rep: any) => {
const weekId = await upcomingWeekId();
const playersWithMultipleSelections = await prisma.player.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
selections: {
some: {
Expand Down Expand Up @@ -114,6 +118,10 @@ export const GetTeamHandler = async (req: any, rep: any) => {
})).sort((p1, p2) => (p2.selection.starting !== p1.selection.starting) ? (p2.selection.starting - p1.selection.starting) : ((p1.positionId || 0) - (p2.positionId || 0)));

const team = await prisma.team.findFirst({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: +req.params.id
},
Expand All @@ -123,6 +131,10 @@ export const GetTeamHandler = async (req: any, rep: any) => {
});

const transfers = await prisma.transfer.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
teamId: +req.params.id,
weekId,
Expand All @@ -133,6 +145,10 @@ export const GetTeamHandler = async (req: any, rep: any) => {

export const GetPointsTeamHandler = async (req: any, rep: any) => {
const team = await prisma.team.findUnique({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: +req.params.id,
}
Expand All @@ -141,6 +157,10 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => {
rep.status(404);
}
const players = await prisma.player.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
include: {
selections: {
where: {
Expand All @@ -166,11 +186,19 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => {
}
});
const deadlineWeek = await prisma.week.findFirst({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: +req.params.weekId
}
});
const transfers = await prisma.transfer.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
teamId: +req.params.id,
weekId: +req.params.weekId,
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { prisma } from "../db/client";

export const GetProfileHandler = async (req: any, rep: any) => {
const user = await prisma.user.findUnique({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: req.user.id
}
Expand All @@ -22,6 +26,10 @@ export const PutUserHandler = async(req: any, rep: any) => {

export const GetTeamsHandler = async (req: any, rep: any) => {
const user = await prisma.user.findUnique({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: req.user.id
},
Expand All @@ -32,6 +40,10 @@ export const GetTeamsHandler = async (req: any, rep: any) => {
}
});
const teams = await prisma.team.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
user: {
id: req.user.id
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/UserAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const GoogleAuthHandler = async (req: AccessTokenRequest, rep: any) => {
const googleUserInfo = await getGoogleUserInfo({ id_token, access_token });
// user has no account yet
let user = await prisma.user.findUnique({
cacheStrategy: {
ttl: 300,
swr: 600,
},
where: {
email: googleUserInfo.email
}
Expand Down
32 changes: 32 additions & 0 deletions src/controllers/Week.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { ProcessState } from "@prisma/client";

export const GetWeeksHandler = async (req: any, rep: any) => {
const weeks = await prisma.week.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
orderBy: {
id: 'asc'
}
Expand All @@ -12,6 +16,10 @@ export const GetWeeksHandler = async (req: any, rep: any) => {

export const GetWeekHandler = async (req: any, rep: any) => {
const week = await prisma.week.findUnique({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
id: +req.params.id
}
Expand Down Expand Up @@ -45,6 +53,10 @@ export const PostWeeksHandler = async (req: any, rep: any) => {

export const PostWeekValidateHandler = async (req: any, rep: any) => {
const statsSumPoints = await prisma.statistic.groupBy({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
match: {
weekId: +req.params.id
Expand All @@ -57,6 +69,10 @@ export const PostWeekValidateHandler = async (req: any, rep: any) => {
});

const teamPoints = await prisma.selection.groupBy({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
weekId: +req.params.id,
starting: 1,
Expand All @@ -68,6 +84,10 @@ export const PostWeekValidateHandler = async (req: any, rep: any) => {
});

const teamsWithSelections = await prisma.team.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
include: {
selections: {
where: {
Expand Down Expand Up @@ -186,11 +206,19 @@ export const DeleteWeekHandler = async (req: any, rep: any) => {
}
export const GetDeadlineInfoHandler = async (req: any, rep: any) => {
const weeks = await prisma.week.findMany({
cacheStrategy: {
ttl: 30,
swr: 60,
},
orderBy: {
id: 'asc'
}
});
const deadlineWeek = await prisma.week.findFirst({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
deadlineDate: {
gt: new Date()
Expand All @@ -201,6 +229,10 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => {
}
});
const displayWeek = await prisma.week.findFirst({
cacheStrategy: {
ttl: 30,
swr: 60,
},
where: {
deadlineDate: {
lt: deadlineWeek?.deadlineDate
Expand Down
3 changes: 2 additions & 1 deletion src/db/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from "@prisma/client";
import { withAccelerate } from '@prisma/extension-accelerate'
export const prisma = new PrismaClient({
// log: ['query', 'info', 'warn', 'error'],
});
}).$extends(withAccelerate());

0 comments on commit 2936244

Please sign in to comment.