diff --git a/prisma/seed.ts b/prisma/seed.ts index aecb040..3d71c56 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,6 +1,4 @@ -import { PrismaClient } from '@prisma/client'; - -const prisma = new PrismaClient(); +import { prisma } from '@/db'; const workshopLocations = [ { name: 'Dubai' }, diff --git a/src/db.ts b/src/db.ts new file mode 100644 index 0000000..a113f04 --- /dev/null +++ b/src/db.ts @@ -0,0 +1,21 @@ +import { PrismaClient } from '@prisma/client'; +import type { Prisma } from '@prisma/client'; +import type { DefaultArgs } from '@prisma/client/runtime/library'; + +const prismaClientSingleton = (): PrismaClient< + Prisma.PrismaClientOptions, + never, + DefaultArgs +> => { + return new PrismaClient(); +}; + +declare const globalThis: { + prismaGlobal: ReturnType; +} & typeof global; + +const prisma = globalThis.prismaGlobal ?? prismaClientSingleton(); + +export { prisma }; + +if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma; diff --git a/src/lib/checkIfCO.tsx b/src/lib/checkIfCO.tsx index 12913f1..604e37e 100644 --- a/src/lib/checkIfCO.tsx +++ b/src/lib/checkIfCO.tsx @@ -1,6 +1,4 @@ -import { PrismaClient } from '@prisma/client'; - -const prisma = new PrismaClient(); +import { prisma } from '@/db'; /** * Checks if a user is a convention organizer diff --git a/src/lib/verifyChallenge.tsx b/src/lib/verifyChallenge.tsx index 7f42ba2..6fe085a 100644 --- a/src/lib/verifyChallenge.tsx +++ b/src/lib/verifyChallenge.tsx @@ -1,6 +1,4 @@ -import { PrismaClient } from '@prisma/client'; - -const prisma = new PrismaClient(); +import { prisma } from '@/db'; /** * Verifies a challenge from the FE for Cardano diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts index 82f9524..3ac68ae 100644 --- a/src/pages/api/auth/[...nextauth].ts +++ b/src/pages/api/auth/[...nextauth].ts @@ -1,14 +1,12 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import NextAuth from 'next-auth'; import type { NextAuthOptions } from 'next-auth'; import CredentialsProvider from 'next-auth/providers/credentials'; import { verifyWallet } from '@/lib/verifyWallet'; -const prisma = new PrismaClient(); - // the shape of the user session object is defined in /types/next-auth.d.ts export const authOptions: NextAuthOptions = { providers: [ diff --git a/src/pages/api/deletePoll/[pollId].ts b/src/pages/api/deletePoll/[pollId].ts index 007d0a6..ae81f0e 100644 --- a/src/pages/api/deletePoll/[pollId].ts +++ b/src/pages/api/deletePoll/[pollId].ts @@ -1,14 +1,12 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { checkIfCO } from '@/lib/checkIfCO'; -const prisma = new PrismaClient(); - type Data = { success: boolean; message: string; diff --git a/src/pages/api/endVoting.ts b/src/pages/api/endVoting.ts index 56368d4..5222bc5 100644 --- a/src/pages/api/endVoting.ts +++ b/src/pages/api/endVoting.ts @@ -1,15 +1,13 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; import { pollPhases } from '@/constants/pollPhases'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { checkIfCO } from '@/lib/checkIfCO'; -const prisma = new PrismaClient(); - type Data = { success: boolean; message: string; diff --git a/src/pages/api/getPoll/[pollId].ts b/src/pages/api/getPoll/[pollId].ts index 9258476..0d7c6c1 100644 --- a/src/pages/api/getPoll/[pollId].ts +++ b/src/pages/api/getPoll/[pollId].ts @@ -1,13 +1,11 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { Poll } from '@/types'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = { poll: Poll | null; message: string }; /** diff --git a/src/pages/api/getPollResults/[pollId].ts b/src/pages/api/getPollResults/[pollId].ts index 7bdcef5..e9900a6 100644 --- a/src/pages/api/getPollResults/[pollId].ts +++ b/src/pages/api/getPollResults/[pollId].ts @@ -1,12 +1,10 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = { votes: { [key: string]: { diff --git a/src/pages/api/getPollVote/[...params].ts b/src/pages/api/getPollVote/[...params].ts index 37a34e6..4c15a55 100644 --- a/src/pages/api/getPollVote/[...params].ts +++ b/src/pages/api/getPollVote/[...params].ts @@ -1,10 +1,8 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; -const prisma = new PrismaClient(); - type Data = { vote: string; message: string }; /** diff --git a/src/pages/api/getPollVoteCount/[pollId].ts b/src/pages/api/getPollVoteCount/[pollId].ts index 46309d1..f35da75 100644 --- a/src/pages/api/getPollVoteCount/[pollId].ts +++ b/src/pages/api/getPollVoteCount/[pollId].ts @@ -1,10 +1,8 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; -const prisma = new PrismaClient(); - type Data = { count: number; message: string }; /** diff --git a/src/pages/api/getPolls.ts b/src/pages/api/getPolls.ts index fc042bf..847302c 100644 --- a/src/pages/api/getPolls.ts +++ b/src/pages/api/getPolls.ts @@ -1,13 +1,11 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { Poll } from '@/types'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = Poll[]; export default async function getPolls( diff --git a/src/pages/api/getRepresentatives.ts b/src/pages/api/getRepresentatives.ts index 44acfd3..eb6882e 100644 --- a/src/pages/api/getRepresentatives.ts +++ b/src/pages/api/getRepresentatives.ts @@ -1,13 +1,11 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { User } from '@/types'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = User[]; /** diff --git a/src/pages/api/getUser/[userId].ts b/src/pages/api/getUser/[userId].ts index b615c19..0885ad7 100644 --- a/src/pages/api/getUser/[userId].ts +++ b/src/pages/api/getUser/[userId].ts @@ -1,13 +1,11 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { User } from '@/types'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = { user: User | null; message: string; diff --git a/src/pages/api/getUserVotes/[userId].ts b/src/pages/api/getUserVotes/[userId].ts index 7aa6654..e5a3273 100644 --- a/src/pages/api/getUserVotes/[userId].ts +++ b/src/pages/api/getUserVotes/[userId].ts @@ -1,14 +1,12 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; import { pollPhases } from '@/constants/pollPhases'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { PollVote } from '@/types'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = { votes: PollVote[]; message: string; diff --git a/src/pages/api/getWorkshopName/[workshopId].ts b/src/pages/api/getWorkshopName/[workshopId].ts index 1b19a69..9a26d6d 100644 --- a/src/pages/api/getWorkshopName/[workshopId].ts +++ b/src/pages/api/getWorkshopName/[workshopId].ts @@ -1,12 +1,10 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = { name: string; message: string; diff --git a/src/pages/api/getWorkshops.ts b/src/pages/api/getWorkshops.ts index e314ecc..9395910 100644 --- a/src/pages/api/getWorkshops.ts +++ b/src/pages/api/getWorkshops.ts @@ -1,13 +1,11 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { Workshop } from '@/types'; import { parseJsonData } from '@/lib/parseJsonData'; -const prisma = new PrismaClient(); - type Data = Workshop[]; /** diff --git a/src/pages/api/newChallenge.ts b/src/pages/api/newChallenge.ts index 05b2299..ba85fb8 100644 --- a/src/pages/api/newChallenge.ts +++ b/src/pages/api/newChallenge.ts @@ -1,11 +1,9 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { PrismaClient } from '@prisma/client'; +import { prisma } from '@/db'; import * as Sentry from '@sentry/nextjs'; import { secureRandom } from '@/lib/secureRandom'; -const prisma = new PrismaClient(); - /** * Generates a unique challenge to include with signature for Cardano * @param req - NextApiRequest diff --git a/src/pages/api/newPoll.ts b/src/pages/api/newPoll.ts index 58bb6f8..673fbf0 100644 --- a/src/pages/api/newPoll.ts +++ b/src/pages/api/newPoll.ts @@ -1,14 +1,12 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { checkIfCO } from '@/lib/checkIfCO'; -const prisma = new PrismaClient(); - type Data = { pollId: string; message?: string; diff --git a/src/pages/api/newPollVote.ts b/src/pages/api/newPollVote.ts index 50bcd6e..759bdee 100644 --- a/src/pages/api/newPollVote.ts +++ b/src/pages/api/newPollVote.ts @@ -1,15 +1,13 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; import { pollPhases } from '@/constants/pollPhases'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { verifyWallet } from '@/lib/verifyWallet'; -const prisma = new PrismaClient(); - type Data = { success: boolean; message: string; diff --git a/src/pages/api/startVoting.ts b/src/pages/api/startVoting.ts index e8db013..2cad01e 100644 --- a/src/pages/api/startVoting.ts +++ b/src/pages/api/startVoting.ts @@ -1,15 +1,13 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; import { pollPhases } from '@/constants/pollPhases'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { checkIfCO } from '@/lib/checkIfCO'; -const prisma = new PrismaClient(); - type Data = { success: boolean; message: string; diff --git a/src/pages/api/updateActiveVoter.ts b/src/pages/api/updateActiveVoter.ts index c5ac291..0e88d75 100644 --- a/src/pages/api/updateActiveVoter.ts +++ b/src/pages/api/updateActiveVoter.ts @@ -1,14 +1,12 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { checkIfCO } from '@/lib/checkIfCO'; -const prisma = new PrismaClient(); - type Data = { userId: string; message: string; diff --git a/src/pages/api/updateUser.ts b/src/pages/api/updateUser.ts index ebb3ce9..4d71699 100644 --- a/src/pages/api/updateUser.ts +++ b/src/pages/api/updateUser.ts @@ -1,14 +1,12 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from 'next'; +import { prisma } from '@/db'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; -import { PrismaClient } from '@prisma/client'; import * as Sentry from '@sentry/nextjs'; import { getServerSession } from 'next-auth'; import { checkIfCO } from '@/lib/checkIfCO'; -const prisma = new PrismaClient(); - type Data = { userId: string; message: string;