-
Hi there, I cannot seem to re-use my mongodb connection between requests. I have the following connect function: import { MongoClient } from 'mongodb';
let mongo: MongoClient;
export const connectToAtlas = async () => {
if (!process.env.ATLAS_CLUSTER_URI) {
throw new Error(`Cannot connect to Atlas no URI in env`);
}
const client = new MongoClient(process.env.ATLAS_CLUSTER_URI);
if (!mongo) {
mongo = await client.connect();
}
return mongo.db(`baggers`);
}; However This is making my app slower than nextjs :( |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
So I followed the example on nextJS https://github.com/vercel/next.js/blob/canary/examples/with-mongodb/lib/mongodb.js |
Beta Was this translation helpful? Give feedback.
-
Remix App Server and Express adapter purge the require cache on every request (only in DEV). You should store your client reference on the 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 = global.__db;
}
export { db }; |
Beta Was this translation helpful? Give feedback.
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.