Next.js API and DB Connections for Typeorm and Mongo #14550
Unanswered
hamdongunner
asked this question in
Help
Replies: 1 comment
-
I agree with what you're saying, it would be ideal to establish the connection only once. Ideally at startup and not when a route is hit. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Should I reconnect to the DB in every handler when I use next.js for API? Or is there a better way to do that?
And isn't that a bad thing if it was the regular approach?
I tried to create a nextjs project with typeorm or MongoDB and all the examples and resources look like they are waiting for the connection with each call.
This image is for typeorm example. I got it from this repo
And here is the Mongo articles that I found that they instruct to add the DB connection in middleware and recall it in every handler.
https://developer.mongodb.com/how-to/nextjs-building-modern-applications https://hoangvvo.com/blog/full-fledged-app-with-next-js-and-mongodb-part-1/
and here is the repos middleware to check it too if you like
https://github.com/kukicado/building-modern-app-with-nextjs-and-mongodb/blob/master/mct/middleware/database.js
https://github.com/hoangvvo/nextjs-mongodb-app/blob/master/middlewares/database.js
Couldn't I just create an entry point to set up my connection for one time so I can keep using it without re-waiting everytime?
Beta Was this translation helpful? Give feedback.
All reactions