Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One connection for each model? #15

Open
yhojann-cl opened this issue Sep 17, 2021 · 9 comments
Open

One connection for each model? #15

yhojann-cl opened this issue Sep 17, 2021 · 9 comments

Comments

@yhojann-cl
Copy link

Is it normal for each entity to have its own connection to the database? if I have 30 objects will I have 30 connections persistently?

@singhpriansh
Copy link

Is it normal for each entity to have its own connection to the database? if I have 30 objects will I have 30 connections persistently?

It's just a quick start you can have one connection made and exported the client through which you can access the db on different code-blocks...

@yhojann-cl
Copy link
Author

It may be a quick start but I have seen many people follow the examples and make a connection for each data model, creating bad practice. I suggest that the examples live up to a real scenario without ceasing to be short examples, for example making a global declaration and sending the handler to the data layers, for example, through an additional variable in the constructor.

@ljhaywar
Copy link
Contributor

Thanks for the feedback! I'll take this into consideration the next time I update the code and article series.

What you'll want to do is create an instance of MongoClient when your app starts and then use that client throughout your app.

@yhojann-cl
Copy link
Author

Yes, I would appreciate it very much and avoid many bad practices of those who are just starting to use mongodb and use these examples as true bases.

@yash-dxt
Copy link

What you'll want to do is create an instance of MongoClient when your app starts and then use that client throughout your app.

Although, how would you handle the case if the mongo connection dies in between the whole course of the application?

@yhojann-cl
Copy link
Author

yhojann-cl commented Dec 26, 2021

What you'll want to do is create an instance of MongoClient when your app starts and then use that client throughout your app.

Although, how would you handle the case if the mongo connection dies in between the whole course of the application?

Related: https://stackoverflow.com/questions/10656574/how-do-i-manage-mongodb-connections-in-a-node-js-web-application/14464750#14464750

But:

This is the correct answer. The accepted answer is very wrong as it says to open a connection pool for each request and then close it after doing so. Terrible architecture.

@ljhaywar
Copy link
Contributor

ljhaywar commented Jan 5, 2022

Although, how would you handle the case if the mongo connection dies in between the whole course of the application?

Without writing the code myself, I'm having trouble visualizing the flow. This is a good question to ask in the Community: community.mongodb.com.

@yash-dxt
Copy link

Look at this piece of code - this works well for me. I've been using it in a production application for some time. I use only one connection throughout the app.

@mizodo
Copy link

mizodo commented Jun 22, 2022

Look at this piece of code - this works well for me. I've been using it in a production application for some time. I use only one connection throughout the app.

Thanks man, this seems to work great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants