Skip to content

Commit

Permalink
Merge pull request #103 from ClearContracts/matt/prisma-client
Browse files Browse the repository at this point in the history
Create prisma singleton
  • Loading branch information
mattlaux authored Nov 11, 2024
2 parents 9940d2e + ca89262 commit a71bbf0
Show file tree
Hide file tree
Showing 23 changed files with 43 additions and 66 deletions.
4 changes: 1 addition & 3 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
import { prisma } from '@/db';

const workshopLocations = [
{ name: 'Dubai' },
Expand Down
21 changes: 21 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -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 prismaClientSingleton>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();

export { prisma };

if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma;
4 changes: 1 addition & 3 deletions src/lib/checkIfCO.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/lib/verifyChallenge.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/deletePoll/[pollId].ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/endVoting.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getPoll/[pollId].ts
Original file line number Diff line number Diff line change
@@ -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 };

/**
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getPollResults/[pollId].ts
Original file line number Diff line number Diff line change
@@ -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]: {
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getPollVote/[...params].ts
Original file line number Diff line number Diff line change
@@ -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 };

/**
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getPollVoteCount/[pollId].ts
Original file line number Diff line number Diff line change
@@ -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 };

/**
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getPolls.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getRepresentatives.ts
Original file line number Diff line number Diff line change
@@ -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[];

/**
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getUser/[userId].ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getUserVotes/[userId].ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getWorkshopName/[workshopId].ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/getWorkshops.ts
Original file line number Diff line number Diff line change
@@ -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[];

/**
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/newChallenge.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/newPoll.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/newPollVote.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/startVoting.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/updateActiveVoter.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/updateUser.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit a71bbf0

Please sign in to comment.