Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prisma client #633

Merged
merged 19 commits into from
Oct 17, 2024
21 changes: 15 additions & 6 deletions formulaire/src/lib/prismaClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
// cf https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices

import { PrismaClient } from "@prisma/client";

const getClient = () => {
return new PrismaClient();
};
declare global {
var prismaClient: PrismaClient | undefined;
}

const client =
globalThis.prismaClient ??
new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL + "&connection_limit=5",
},
},
log: ["info", "warn", "error"],
});

const client = getClient();
globalThis.prismaClient = client;

export default client;
28 changes: 12 additions & 16 deletions src/lib/prismaClient.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
// cf https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices

import { PrismaClient } from "@prisma/client";

declare global {
// eslint-disable-next-line no-var
var prisma: PrismaClient | null;
// function getPrismaClient(): PrismaClient;
var prismaClient: PrismaClient | undefined;
}

const getClient = () => {
if (process.env.NODE_ENV === "production") {
return new PrismaClient();
} else {
if (!global.prisma) global.prisma = new PrismaClient();
return global.prisma;
}
};

const client = getClient();
export const client =
globalThis.prismaClient ??
new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL + "&connection_limit=5",
},
},
log: ["info", "warn", "error"],
});

export default client;
globalThis.prismaClient = client;
3 changes: 1 addition & 2 deletions src/pages/api/commentaires/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { NextApiHandler, NextApiRequest } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/commissions/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { withSentry } from "@sentry/nextjs";
import type { NextApiHandler, NextApiRequest } from "next";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const { id: commissionIdStr } = req.query;
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/commissions/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { withSentry } from "@sentry/nextjs";
import type { NextApiHandler } from "next";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
if (req.method == "GET") {
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/commissions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { NextApiHandler, NextApiRequest } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down Expand Up @@ -42,7 +41,6 @@ const get: NextApiHandler = async (req, res) => {
? await getUpcomingCommissionsNotEmpty(req)
: await getUpcomingCommissions()
: await getUpcomingCommissionsByDepartement(departements as string);
await client?.$disconnect()
res.status(200).json(superjson.stringify(commissions));
};

Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/commissions/upcoming/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { withSentry } from "@sentry/nextjs";
import type { NextApiHandler } from "next";
import { getSession } from "next-auth/react";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/dossiers/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type { TransitionEvent } from "src/lib/statutDossierStateMachine";
import { factory as statutDossierStateMachineFactory } from "src/lib/statutDossierStateMachine";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient"

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/enfant/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { NextApiHandler, NextApiRequest } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/enfants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { NextApiHandler } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/search.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { getSession } from "next-auth/react";
import { searchDossiers, searchEnfants } from "src/lib/queries";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
if (req.method !== "GET") {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/sendlist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { NextApiHandler, NextApiRequest } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/sync/inc/dossiers/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { withSentry } from "@sentry/nextjs";
import type { NextApiHandler, NextApiRequest } from "next";

import { PrismaClient, Prisma } from "@prisma/client";
const client = new PrismaClient();
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
if (req.method === "GET") {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/sync/inc/dossiers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
import { frenchDateText, frenchDepartementName } from "src/lib/helpers";
import type { z } from "zod";

import { PrismaClient, Prisma } from "@prisma/client";
const client = new PrismaClient();
import { client } from "src/lib/prismaClient";

export const config = {
api: {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/sync/inc/enfant/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { withSentry } from "@sentry/nextjs";
import type { NextApiHandler, NextApiRequest } from "next";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
if (req.method == "DELETE") {
Expand Down
7 changes: 2 additions & 5 deletions src/pages/api/users/id/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiHandler } from "next";
import { PrismaClient, User } from "@prisma/client";
import { client } from "src/lib/prismaClient";
import { getSession } from "next-auth/react";

const handler: NextApiHandler = async (req, res) => {
Expand All @@ -17,7 +17,6 @@ const handler: NextApiHandler = async (req, res) => {
};

const getUserById: NextApiHandler = async (req, res) => {
const prisma = new PrismaClient();
let userIds: number[] | number = [];

if (req.query.id) {
Expand All @@ -30,17 +29,15 @@ const getUserById: NextApiHandler = async (req, res) => {
}
}
try {
const users = await prisma.user.findMany({
const users = await client.user.findMany({
where: {
id: {
in: userIds,
},
},
});
await prisma?.$disconnect();
res.status(200).json(users);
} catch (e: unknown) {
await prisma?.$disconnect();
console.log(e);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { NextApiHandler } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma, Role } from '@prisma/client'
const client = new PrismaClient()
import { Role } from "@prisma/client";
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/users/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { NextApiHandler } from "next";
import { getSession } from "next-auth/react";
import superjson from "superjson";

import { PrismaClient, Prisma } from '@prisma/client'
const client = new PrismaClient()
import { client } from "src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
Expand Down
Loading