Skip to content

Commit 42f2f80

Browse files
fix: instantiate once for each handler (#565)
1 parent e7c87e7 commit 42f2f80

20 files changed

Lines changed: 133 additions & 139 deletions

File tree

.kube-workflow/env/prod/values.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
app:
22
addVolumes:
33
- docs
4-
replicas: 1 # temporary, need to delete when swr fixed
54
app-form:
65
addVolumes:
76
- docs-form
8-
replicas: 1 # temporary, need to delete when swr fixed
97

108
metabase:
119
enabled: true

package-lock.json

Lines changed: 35 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
},
3131
"dependencies": {
3232
"@dataesr/react-dsfr": "^0.9.2",
33-
"@next-auth/prisma-adapter": "^1.0.0",
34-
"@prisma/client": "^3.6.0",
33+
"@next-auth/prisma-adapter": "^1.0.5",
34+
"@prisma/client": "^4.7.0",
3535
"@sentry/nextjs": "^6.15.0",
3636
"@socialgouv/matomo-next": "^1.6.1",
3737
"b64-to-blob": "^1.2.19",
@@ -89,7 +89,7 @@
8989
"maildev": "^1.1.0",
9090
"mjml": "^4.11.0",
9191
"prettier": "^2.5.1",
92-
"prisma": "^3.6.0",
92+
"prisma": "^4.7.0",
9393
"slugify": "^1.6.3",
9494
"ts-node": "^10.4.0",
9595
"typescript": "4.5.2"

src/pages/api/commentaires/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import type { Commentaire } from "@prisma/client";
21
import { withSentry } from "@sentry/nextjs";
32
import type { NextApiHandler, NextApiRequest } from "next";
43
import { getSession } from "next-auth/react";
5-
import prisma from "src/lib/prismaClient";
64
import superjson from "superjson";
75

6+
import { PrismaClient, Prisma } from '@prisma/client'
7+
const client = new PrismaClient()
8+
89
const handler: NextApiHandler = async (req, res) => {
910
const session = await getSession({ req });
1011
if (!session) {
@@ -31,21 +32,19 @@ function getId(req: NextApiRequest): number {
3132
const remove: NextApiHandler = async (req, res) => {
3233
const commentId = Number(req.body as string);
3334
try {
34-
await prisma.commentaire.delete({
35+
await client.commentaire.delete({
3536
where: { id: commentId },
3637
});
37-
await prisma?.$disconnect()
3838
res.status(200).json({ message: "Commentaire supprimé" });
3939
} catch (e: unknown) {
40-
await prisma?.$disconnect()
4140
console.log(e);
4241
res.status(200).json({ message: "Commentaire non trouve" });
4342
}
4443
};
4544

4645
const get: NextApiHandler = async (req, res) => {
4746
const dossierId = getId(req);
48-
const allComments = await prisma.commentaire.findMany({
47+
const allComments = await client.commentaire.findMany({
4948
include: {
5049
user: true,
5150
},
@@ -54,14 +53,13 @@ const get: NextApiHandler = async (req, res) => {
5453
dossierId: dossierId,
5554
},
5655
});
57-
await prisma?.$disconnect()
5856
res.status(200).json(superjson.stringify(allComments));
5957
};
6058

6159
const post: NextApiHandler = async (req, res) => {
6260
const data = JSON.parse(req.body as string);
6361
try {
64-
await prisma.commentaire.create({ data });
62+
await client.commentaire.create({ data });
6563
} catch (e: unknown) {
6664
console.log(e);
6765
}

src/pages/api/commissions/[id].ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { withSentry } from "@sentry/nextjs";
22
import type { NextApiHandler, NextApiRequest } from "next";
3-
//import { getSession } from "next-auth/react";
4-
import prisma from "src/lib/prismaClient";
53
import superjson from "superjson";
64

5+
import { PrismaClient, Prisma } from '@prisma/client'
6+
const client = new PrismaClient()
7+
78
const handler: NextApiHandler = async (req, res) => {
8-
/*const session = await getSession({ req });
9-
if (!session) {
10-
res.status(401).end();
11-
return;
12-
}*/
139
const { id: commissionIdStr } = req.query;
1410
if (typeof commissionIdStr !== "string") {
1511
res.status(404).send(`not a valid commission id`);
@@ -32,7 +28,7 @@ function getId(req: NextApiRequest): number {
3228

3329
const get: NextApiHandler = async (req, res) => {
3430
const id = getId(req);
35-
const commission = await prisma.commission.findUnique({
31+
const commission = await client.commission.findUnique({
3632
include: {
3733
dossiers: {
3834
include: {
@@ -47,7 +43,6 @@ const get: NextApiHandler = async (req, res) => {
4743
},
4844
where: { id },
4945
});
50-
await prisma?.$disconnect()
5146

5247
res.status(200).json(superjson.stringify(commission));
5348
};
@@ -78,11 +73,10 @@ const update: NextApiHandler = async (req, res) => {
7873
updates.dateLimiteDepot = parsed.dateLimiteDepot;
7974
}
8075

81-
const updateCommission = await prisma.commission.update({
76+
const updateCommission = await client.commission.update({
8277
data: updates,
8378
where: { id: parsed.id },
8479
});
85-
await prisma?.$disconnect()
8680

8781
res.status(200).json(superjson.stringify(updateCommission));
8882
};

src/pages/api/commissions/date/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { withSentry } from "@sentry/nextjs";
22
import type { NextApiHandler } from "next";
3-
import prisma from "src/lib/prismaClient";
43
import superjson from "superjson";
54

5+
import { PrismaClient, Prisma } from '@prisma/client'
6+
const client = new PrismaClient()
7+
68
const handler: NextApiHandler = async (req, res) => {
79
if (req.method == "GET") {
810
await get(req, res);
@@ -13,12 +15,11 @@ const handler: NextApiHandler = async (req, res) => {
1315
};
1416
const get: NextApiHandler = async (req, res) => {
1517
const commissions = await getUpcomingCommissions();
16-
await prisma?.$disconnect()
1718
res.status(200).json(superjson.stringify(commissions));
1819
};
1920

2021
const getUpcomingCommissions = async () => {
21-
return prisma.commission.findMany({
22+
return client.commission.findMany({
2223
orderBy: { date: "asc" },
2324
where: {
2425
date: { gte: new Date() },

src/pages/api/commissions/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { withSentry } from "@sentry/nextjs";
22
import type { NextApiHandler, NextApiRequest } from "next";
33
import { getSession } from "next-auth/react";
4-
import prisma from "src/lib/prismaClient";
54
import superjson from "superjson";
65

6+
import { PrismaClient, Prisma } from '@prisma/client'
7+
const client = new PrismaClient()
8+
79
const handler: NextApiHandler = async (req, res) => {
810
const session = await getSession({ req });
911
if (
@@ -40,14 +42,14 @@ const get: NextApiHandler = async (req, res) => {
4042
? await getUpcomingCommissionsNotEmpty(req)
4143
: await getUpcomingCommissions()
4244
: await getUpcomingCommissionsByDepartement(departements as string);
43-
await prisma?.$disconnect()
45+
await client?.$disconnect()
4446
res.status(200).json(superjson.stringify(commissions));
4547
};
4648

4749
const post: NextApiHandler = async (req, res) => {
4850
const data = JSON.parse(req.body as string);
4951
try {
50-
await prisma?.commission.create({ data });
52+
await client?.commission.create({ data });
5153
} catch (e: unknown) {
5254
console.log(e);
5355
}
@@ -57,7 +59,7 @@ const post: NextApiHandler = async (req, res) => {
5759
const remove: NextApiHandler = async (req, res) => {
5860
const commissionId = Number(req.body as string);
5961
try {
60-
await prisma?.commission.delete({
62+
await client?.commission.delete({
6163
where: { id: commissionId },
6264
});
6365
res.status(200).json({ message: "Commission supprimée" });
@@ -69,7 +71,7 @@ const remove: NextApiHandler = async (req, res) => {
6971

7072
const getUpcomingCommissions = async () => {
7173
console.log('upcoming')
72-
return prisma?.commission.findMany({
74+
return client?.commission.findMany({
7375
include: {
7476
dossiers: {
7577
include: {
@@ -88,7 +90,7 @@ const getUpcomingCommissions = async () => {
8890
const getUpcomingCommissionsNotEmpty = async (req: NextApiRequest) => {
8991
const session = await getSession({ req });
9092
console.log('upcoming not empty !!!')
91-
return await prisma?.commission.findMany({
93+
return await client?.commission.findMany({
9294
include: {
9395
dossiers: {
9496
where: session?.dbUser.role !== "MEDECIN" ?
@@ -152,7 +154,7 @@ const getUpcomingCommissionsNotEmpty = async (req: NextApiRequest) => {
152154
const getUpcomingCommissionsByDepartement = async (departements: string) => {
153155
console.log('upcoming by departement')
154156
console.log("departements : ", departements.split(","));
155-
return prisma.commission.findMany({
157+
return client.commission.findMany({
156158
include: {
157159
dossiers: {
158160
include: {
@@ -177,7 +179,7 @@ const getUpcomingCommissionsByDepartement = async (departements: string) => {
177179

178180
const getPastCommissions = async () => {
179181
console.log('past commissions')
180-
return prisma.commission.findMany({
182+
return client.commission.findMany({
181183
include: {
182184
dossiers: {
183185
include: {

0 commit comments

Comments
 (0)