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: remove global prisma (experiment) #333

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/src/router/chats.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { prisma } from "@oakai/db/client";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codeincontext if we don't want to change the global import behaviour, we need to add this anyway. This file isn't importing prisma at the moment because of the declare global in db/client.

I'm not sure how this isn't throwing an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, very odd. Maybe we don't actually call these endpoints? I've approved #342

import * as Sentry from "@sentry/nextjs";
import { TRPCError } from "@trpc/server";
import { isTruthy } from "remeda";
Expand Down
27 changes: 11 additions & 16 deletions packages/db/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@ const createPrismaClient = () => {
const extendedPrisma = createPrismaClient();
export type PrismaClientWithAccelerate = typeof extendedPrisma;

declare global {
// allow global `var` declarations
// eslint-disable-next-line no-var
var prisma: PrismaClientWithAccelerate | undefined;
}

let prisma: PrismaClientWithAccelerate;

if (process.env.NODE_ENV === "production") {
prisma = createPrismaClient();
} else {
if (!global.prisma) {
global.prisma = createPrismaClient();
let prismaInstance: PrismaClientWithAccelerate | undefined = undefined;

const getPrismaClient = () => {
if (prismaInstance) {
return prismaInstance;
}
prisma = global.prisma;
}

export { prisma };
prismaInstance = createPrismaClient();

return prismaInstance;
};

export const prisma = getPrismaClient();
Loading