Skip to content

Commit

Permalink
enable singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
matmut7 committed Oct 10, 2024
1 parent 664c7e4 commit 4cef95c
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 40 deletions.
23 changes: 7 additions & 16 deletions src/lib/prismaClient.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
// 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({
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
3 changes: 1 addition & 2 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
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
4 changes: 2 additions & 2 deletions src/pages/api/dossiers/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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 { PrismaClient, Prisma } from "@prisma/client";
const client = new 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/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
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

0 comments on commit 4cef95c

Please sign in to comment.