Next.js Discord

Discord Forum

How to use MongoDB in Vercel

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
If I just want to fetch data from MongoDB, should I use Mongodb-client or Mongoose. Where should I put the server file? My project root directory is:
/app
/api
/auth/[...nextauth]
route.js
/chat
route.js
/chat
/share
/asset

9 Replies

Cape lionOP
So I am using this template: https://github.com/vercel-labs/ai-chatbot
Tramp ant
Id make a root /db dir. choosing between clients is really your call.

Mongoose is usually a bit heavier, not ideal if youre hosting serverless

I personally go for mongodb (the official driver) with typescript typings but mongoose has some benefits like middlewares and hooks that allow pre and post processing. Looking into prisma is also a good idea but a bit time consuming to learn
Cape lionOP
For the template I am referring to, it seems that all of it is using edge runtime. And I tried to use MongoDB-client. But I found that MongoDB-client cannot be used because Edge runtime doesn't support some of its NodeJS modules.
Tramp ant
ive seen people opt for prisma when using edge
havent been able to personally give it a go tho
Tramp ant
in a nodejs environment, each api request would initiate a new instance of the function so you have to initiate the db client within the function using it. (which is why at some point caching the connection or using a proxy is necessary for heavy traffic) This is a good walkthrough https://www.mongodb.com/developer/languages/javascript/nextjs-with-mongodb/.

when using the edge runtime env i think prisma proxy is the way to go.
Cape lionOP
thanx!!!