Giter VIP home page Giter VIP logo

Comments (2)

WonderPanda avatar WonderPanda commented on May 22, 2024

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.

WonderPanda avatar WonderPanda commented on May 22, 2024

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/dynamic-example/dynamic-example.module.ts

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.