SierraSoftworks / Iridium

A high performance MongoDB ORM for Node.js
http://sierrasoftworks.github.io/Iridium/
Other
569 stars 25 forks source link

Question: Managing connections in Iridium #102

Open mas-dude opened 7 years ago

mas-dude commented 7 years ago

What is the best way to manage connections when using Iridium with Express? Should I be opening a single connection on app start and then use that for the life of the application or should I be creating a connection per request?

I have been following this sample which uses Iridium and creates a connection per repository operation but contradicts this advice.

Just wanting to make sure if there is anything in Iridium that might be doing something different.

notheotherben commented 7 years ago

Hi @mas-dude, you should ideally be holding open a connection for the duration of your application's life cycle since the underlying MongoDB driver manages connection reuse and pooling automatically.

With inversify, you would probably be best off injecting a connected Iridium instance singleton into your repositories. This also avoids the startup cost of building your models and improves V8's ability to optimise bytecode for your specific models.

mas-dude commented 7 years ago

Thanks @SPARTAN563. That is very helpful.

CatGuardian commented 6 years ago

I had this exact same question. Might be worth addressing directly in the documentation.

Kaffiend commented 6 years ago

I've forked the example repository in question that you were referring to as a sample, I've updated its dependencies and added socket.io injection. I will update it to implement this best practice, as i've done so with our closed source professional projects.