John is a member of the NestJS core team
So you've noticed the cool ways in which you can dynamically configure some of the out-of-the-box NestJS ...
For further actions, you may consider blocking this person and/or reporting abuse
Great article! i think it's worth showing the interface of
MassiveConnectAsyncOptions
as i feel it's a missing piece@gimboya, Thank you for the feedback.
I am in the midst of a large project so won't have time to update the article at the moment, but appreciate your suggestion. I'm sure you probably found it, but just in case, you can view the interface here: github.com/nestjsplus/massive/blob.... Here's what it looks like:
Yup got it! and very much appreciate it. I am exited that I am starting to grasp dynamic modules
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 multipleMassiveService
instance? If so, how would we inject the right one at the right place?@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 thedb
object I briefly mention actually represents a connection pool. So the recommended pattern for Massive (and anypg-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!
In the section about Supporting Multiple Async Options Providers Techniques, in the first block of code, I think there a slight mistake in the code.
This part:
should be:
Good catch! Thanks for reporting!
Wow, that's a lot of code. And what is the benefit of this? In my opinion, it is better to transfer the metadata for the database connection synchronously:
Pass this config to DI:
And when it's time to execute requests to the database, then make an asynchronous connection to the database. This approach allows, in addition to simplifying the code, to reconnect to a database when needed:
Thank you for this!
Your NestJS module for Massive was actually my introduction to MassiveJS, which was incredibly impressive. It really does hit the sweet spot for database interaction - avoiding the heaviness of an ORM while still providing some elegant interactivity.
I was wondering, do you have a preferred approach for utilizing TypeScript with Massive in your NestJS projects?
Such a great article, thanks!
Glad you found it helpful @mustapha !
@johnbiundo I cant find your e-mail address anywhere so I hope you read this comment. There was a comment already asking for the question I have, but you didnt answer it. This already took me hours and hours, days and days, but I cant find anything online, so I hope you might help!
When my module has other imports that need configuration (some examples are HttpModule, CacheModule, ClientsModule) and I want them to be configured by the options that are provided to my module (as you described in the article) but the provided options are not available to them.
Maybe try to import HttpModule to your MassiveModule and try to configure a value of it (e.g. timeout) by using one of your 3 providers. But I also made a simple sandbox repository to figure out a solution. Feel free to try it out there or even make a PR: github.com/MickL/nestjs-dynamic-mo...
There is also a question on Stackoverflow asking the exact same question 6 months ago: stackoverflow.com/questions/633564...
THANK YOU VERY MUCH! :)
when using an import (say HttpModule) from within the register.
How would you achieve passing your options parameters to the HttpModule import on the registerAsync method?
I have the exact same question / problem and struggling for hours and hours now... :(
Question SO: stackoverflow.com/questions/633564...
Repo to try things out: github.com/MickL/nestjs-inject-exi...
thank you for the great article, I spend a lot of time to create the share module with async style.
Glad it helped! Thanks for the feedback.
Thanks for sharing. I have translated it into Chinese. Can I post it on my blog?
Is it possible to log knex queries and insert into database or any sort of logger file?
Very easy to understand dynamic module concept in NestJS, Thank you very much
Thanks for the post!. Is there a way where we can pass configuration thru api and dynamically create multiple database.