DEV Community

Discussion on: Advanced NestJS: How to build completely dynamic NestJS modules

Collapse
 
fwoelffel profile image
Frédéric Woelffel

Thanks for this post @johnbiundo đź‘Ź
It surely provides a good example of how to develop our own dynamic modules. I'm wondering how you would handle multiple database connections with this MassiveModule? Wouldn't you have to provide multiple MassiveService instance? If so, how would we inject the right one at the right place?

Collapse
 
johnbiundo profile image
John Biundo

@fwoelffel Thank you for your feedback!

Right, I didn't really cover that in this article, but good question. The Massive library uses pg-promise under the covers and the db object I briefly mention actually represents a connection pool. So the recommended pattern for Massive (and any pg-promise library) is to use a singleton connection object which manages the connection pool under the covers. The full Massive integration library I built has additional options to configure the connection pool size and some other parameters to let you fine tune.

Oh, and there's a tiny bit more plumbing in the full library. Basically, it builds an injection token representing the shared connection object, and to use it in any module, you just inject that token. I tried to cover that in the @nestjsplus/massive docs, but let me know if it's still not clear. I felt like this detail - while relevant to understanding the full MassiveJS library - was a bit too distracting to cover in this article, but I'm not surprised you picked up on it!

Hope that answers the question!