Skip to content

Reuse database connection #2198

Closed Answered by kiliman
dan-cooke asked this question in Q&A
Mar 3, 2022 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

Remix App Server and Express adapter purge the require cache on every request (only in DEV).

You should store your client reference on the global object. Here's how the Prisma client is persisted from the Jokes tutorial.

let db: PrismaClient;

declare global {
  var __db: PrismaClient | undefined;
}

// this is needed because in development we don't want to restart
// the server with every change, but we want to make sure we don't
// create a new connection to the DB with every change either.
if (process.env.NODE_ENV === "production") {
  db = new PrismaClient();
  db.$connect();
} else {
  if (!global.__db) {
    global.__db = new PrismaClient();
    global.__db.$connect();
  }
  db = gl…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@kiliman
Comment options

@dan-cooke
Comment options

@kiliman
Comment options

Comment options

You must be logged in to vote
2 replies
@dan-cooke
Comment options

@MHassanNadeem
Comment options

Answer selected by dan-cooke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants