Comments (2)
Looking further into this I feel like if your goal is to have multiple injectables provided from your dynamic module that the current APIs support that relatively easily. The boilerplate that this library removes is for the configuration of your Dynamic Module, not for what it will provide. So in that sense you can pass as complex an object as you want under one Injection token for the configuration.
Check out this snippet from the RabbitMQ library:
export class RabbitMQModule
extends createConfigurableDynamicRootModule<RabbitMQModule, RabbitMQConfig>(
RABBIT_CONFIG_TOKEN,
{
providers: [
{
provide: AmqpConnection,
useFactory: async (
config: RabbitMQConfig
): Promise<AmqpConnection> => {
const connection = new AmqpConnection(config);
await connection.init();
const logger = new Logger(RabbitMQModule.name);
logger.log('Successfully connected to RabbitMQ');
return connection;
},
inject: [RABBIT_CONFIG_TOKEN]
}
],
exports: [AmqpConnection]
}
)
You can see that it has one provider which is implemented with a factory function which relies on the Config object (in this case RabbitMQConfig
) to build up and provide an AmqpConnection
.
If the config object for this dynamic module were more complex (with multiple top level object keys that each were responsible for configuring an individual provider), I could easily add more providers to the array in the above snippet and have each of them return something else based on the Config object:
export class RabbitMQModule
extends createConfigurableDynamicRootModule<RabbitMQModule, RabbitMQConfig>(
RABBIT_CONFIG_TOKEN,
{
providers: [
{
provide: AmqpConnection,
useFactory: async (
config: RabbitMQConfig
): Promise<AmqpConnection> => {
const connection = new AmqpConnection(config);
await connection.init();
const logger = new Logger(RabbitMQModule.name);
logger.log('Successfully connected to RabbitMQ');
return connection;
},
inject: [RABBIT_CONFIG_TOKEN]
},
{
provide: AmqpConnection,
useFactory: async (
config: RabbitMQConfig
): Promise<SomeOtherThing> => {
// do something with config
return new SomeOtherThing();
},
inject: [RABBIT_CONFIG_TOKEN]
}
],
exports: [AmqpConnection]
}
)
I'm not sure I see the use case for needing to inject the configuration options to other places after the fact as generally it's simply used to bootstrap the dynamic module properly but you could easily inject the config object and then just access the property on it that you need eg options.service
in your example or you could even use the snippets I've provided as a starting point to provide them dynamically using providers as a means of destructuring the top level config object into discrete parts each using it's own new Injectable token.
Let me know if this allows you to accomplish the scenario that you're after
from nestjs.
I made an example app so you can see it in action. You can pull it down and run it but the relevant files are here:
https://github.com/WonderPanda/dynamic-modules-example/blob/master/src/app.module.ts
https://github.com/WonderPanda/dynamic-modules-example/blob/master/src/app.controller.ts
Feel free to re-open this if you feel this doesn't meet your needs
from nestjs.
Related Issues (20)
- @golevelup/nestjs-rabbitmq- error when publish message: Channel closed
- ts-jest - When function is called with a mock, toHaveBeenCalledWith() always succeed even when it should not
- rabbitmq: allow per subscriber/handler deserializer customization HOT 1
- RabbitMQ AmqpConnection access from child module
- Wildcard routing stopped working. RabbitMQ
- rabbitmq: support specifying exchange per routing key in RabbitSubscribe
- Single Channel Configuration Across Multiple @RabbitSubscribe Decorators Not Working as Expected
- No delivery mode option on AmqpConnection.request method HOT 2
- RabbitMQ: RabbitSubscribe using a handler name without corresponding handler config asserts randomized queues HOT 3
- rabbitmq: consumerTags are not cleaned up on connection/channel disconnect
- rabbitmq: canceled consumers are resumed if connection/channel reconnects HOT 1
- rabbitmq: direct sending to queue without the need for exchanges HOT 1
- rabbitmq: binding a queue to exchange without a routingKey
- ts-jest: Cannot create mock of a class which has private dependencies in constructor
- rabbitmq: handlers didn't works in app-non-pub HOT 1
- golevelup/nestjs-stripe: project can not compile
- Stripe Test Webhook
- Stripe webhooks - "undefined" is not valid JSON HOT 2
- How should I use this within RabbitSubscribe method?
- ts-vitest: using with `vi.spyOn` isn't working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs.