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
How to create dynamic, async, and configurable modules (chapter) #223
Comments
@kamilmysliwiec Any update on this ? Or a simple example ? Still confused how to inject ? I need to provide Services to my module using useFactory and inject ? |
Bump |
Any updates on this @kamilmysliwiec? |
|
@webberwang As you can see here the interface inherits the |
@BrunnerLivio ah! thanks for that |
With the updated Custom Providers chapter, I think we can close this. I'll give it a couple of days to see if there are any takers for remaining questions. |
It is still not clear how to implement own
I want it to work in the same way as
And after a digging documentation for a while, I still can not find any suggestion how to do that. It would be cool to add code sample to official documentation. Thank you! |
For all who struggle with it, here is working solution (you don't need any In my example we push firebase credentials from the outside of module. Constant somewhere:
FirebaseService:
Sub-module, which provides FirebaseService:
Add in
Hope it will help to someone |
@AndyGura I've been struggling with this for days and it feels like I'm so close now that you made this comment. I'm having trouble with this syntax:
normally, this would have a "provide" property and a "useClass" or "useFactory" as the next argument, but you're using a spread operator before the factory. When I use your syntax, I get the following error (I'm using this for a Salesforce connection, but the concept seems to be the same, I just need values from the @nestjs/config service):
if I use "useFactory" instead of the spread I get this error: The parts of my app that match yours are: SalesforceService
Sub-module which provides SalesForceService:
super-module:
I'm also not entirely clear on how the "inject" property in the above "inject: [ConfigService]" is used on the receiving side (sub-module). Any help you can provide would be awesome, THANKS! |
@ShiftyMcCool Looks like you should use spread operator on Since you have
Code
Will create the valid structure in providers:
Exactly this approach worked for me. Hope it will help you |
@AndyGura Thank you so much for your quick reply! I was just able to try your suggestions and it worked! I had to make one change though. After I fixed the useFactory/sfFactory switch (nice catch), I was still getting the
Not sure if this could be the issue, but I'm developing my SalesForce module as an NPM package that is being used in a NestJS app. Doesn't seem to me that it would cause any issues, but I thought I would mention it for anybody else looking for a solution to this issue. Thanks again @AndyGura, you saved my butt 😁 P.S. |
@ShiftyMcCool I just tested your method of moving the For example, consider this:
This will create two separate instances of JwtService, which is not what I want. Also, doing the following does not work at all, it will cause JwtService to be null/undefined in UsersService:
Does anyone have any idea/solution for how to acheive this kind of inheritance, without having to make AuthModule global, and without having to import AuthModule.forRoot() everywhere I need it? |
I'm not sure why this thread is closed; I think in general the NestJs framework STILL lacks both documentation AND methods for clearly and concisely setting the DI lifetime/scope of modules. It is confusing for both new and seasoned developers to try to understand the relationship between module lifetimes and provider lifetimes and both can get mixed up/confused easily. Also, the One such complexity that is hard to grasp is that when you are importing a module with To add to the complexity there are It is also extremely hard to debug the IoC Container (what is injected, instantiated, where etc.) in NestJs. I still haven't found a way how, except through something like nestjs-spelunker which, overall, is not a great debugging experience. I think at a minimum, if you choose to go this direction with module configuration, the framework should provide an interface with these methods that modules implement. However, I still fundamentally disagree with this direction as it shouldn't be this complicated to simply want to pass in some data to a module. Edit: |
The documentation provided for this seems over complicated and largely irrelevant to the basic desired use case:
Bluntly: you can't. The module configuration cannot access the config service. However, that's probably not what you actually want. If you look carefully at what https://github.com/nestjsplus/massive/blob/master/src/massive.module.ts does, you'll see that what it does is it creates a set of dynamic providers, that can be injected into services at runtime. To do this, you register a factory function, that returns the provider value and can take injected values, like this:
That's the only way you can do this. That's what all the implementations do. The documentation and examples around this do a lot of back and forth with options and options factory providers and so on, but basically, you just do this:
Note: Now, you can extend this to take options with a custom factory function and a custom set of inject values, but, honestly, you almost never want to do that. Don't do it. It's unnecessary over complication unless you're writing a library; if you're just configuring your own modules, just do it is simply, as shown. |
Creating dynamic, async, and configurable modules will become significantly simpler in v9 thanks to this feature nestjs/nest#9534. Docs will be updated very soon. |
I'm submitting a...
Current behavior
Expected behavior
Ref nestjs/nest#1415
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: