Next.js Discord

Discord Forum

NextJs MongoDb Connection

Unanswered
raut008 posted this in #help-forum
Open in Discord
I want to establish connection with MongoDB When I do nom run dev or bol run start. Rather than establishing connection every time I hit a next api route.

https://github.com/mongodb-developer/nextjs-with-mongodb/blob/main/pages/api/movies.js

Like the client promise here will be imported in every next api route. How can I achieve this ?

10 Replies

you need to to create a database connection file like below import { MongoClient } from 'mongodb';
let client;
if (!client) {
client = new MongoClient(process.env.MONGODB_URI);
if (!client.isConnected()) {
client.connect();
}
} then you call this function in api's whenever you want to create database connection.
I understood these things but It will make a connection when I hit a Next API route. But I want to establish connection just after npm run start without have to hit Next API route
@raut008 I understood these things but It will make a connection when I hit a Next API route. But I want to establish connection just after npm run start without have to hit Next API route
Lilac
dev mode nextjs only compiles on demand (and obv it should connect to the db or check for any existing connection only where it is needed ).
you might want to run mongoose.connect only when it is needed, for example call the function inside /xyz it will connect when you visit /xyz in the browser.
Let's say In case of express server Connection is established with the database when I run npm run start. Similar if I establish connection just after npm run start it will be beneficial in terms to time taken , Correct me if I'm wrong.
Also If my have /xyz and /abc I'll be calling clientPromise in both the api routes. Which I want to avoid.
It might help in scenario where if establishing the connection fails I'll come to know about it during npm run start itself whereas if above mentioned scenario I'll come to know about it after hitting a api route
You can acheive that by tracking the mongodb connection status in a method. When your server will start this method will check that whether mongoddb is connected or not , if not connected then this method will establish the connection.Thats it.
Can you give me a example for it ?
I want to establish MongoDb Connection server on NextJs server bootstrap
Use while loop and check if mongodb is not connected then make mongodb connection in while loop. Thats it. To create such example I need to know whether you are using mongoose or mongodb driver. Howver if you spent little time, you will find everything in their docs.