nuxt-mongoose icon indicating copy to clipboard operation
nuxt-mongoose copied to clipboard

Connect to MongoDB lazy

Open MickL opened this issue 1 year ago • 3 comments

Hello, right now it seems like a connection to MongoDB is established when the Nuxt app starts. But when the app is deployed as a serverless function maybe a server handler is called that doesnt require any MongoDB connection. In this case the start of the app is delayed even tho MongoDB isnt even needed for the incoming request.

Is it possible to only connect to MongoDB when a query is executed?

For example:

  • GET api/cats -> returns data from a third party service -> No MongoDB connection is established
  • GET api/dogs -> calls model Dog.find() —> Now a MongoDB connection gets established

MickL avatar Nov 25 '23 14:11 MickL

@MickL that's a pretty good idea! for sure it would be nice. thanks

arashsheyda avatar Nov 25 '23 18:11 arashsheyda

I want to upvote this, it's something I just wanted to ask about 😀 Is there some way we could help with it? Do you have some idea how you would like to implement it?

jankaderabek avatar Nov 29 '23 07:11 jankaderabek

@jankaderabek I tought if we could find the right hook for nitr it's possible but I couldn't find any to work, so I'm kinda stuck

arashsheyda avatar Dec 05 '23 22:12 arashsheyda