Next.js Discord

Discord Forum

Using connection pool in nextjs

Unanswered
Alaskan Malamute posted this in #help-forum
Open in Discord
Alaskan MalamuteOP
Hi! I was just wondering how can I efficiently connect to database in next.js.
I mean, there's no "starting" file, where I can just connect to db & create event listeners as I can in express.js 🤷‍♂️

await mongoose.connect(process.env.DATABASE_URI as string, {
  maxPoolSize: config.database.maxPoolSize,
});


I know it may sound stupid but I just wanna implement connection pool so I don't need to connect & disconnect every single time
Thanks in advance!

2 Replies

Alaskan MalamuteOP
sooo I just implemented it as a singleton, idk if that's a good practice but here we are
I think the commonly excepted method is to have one file with the connection and all the files import that one