GithubHelp home page GithubHelp logo

Comments (9)

jmcdo29 avatar jmcdo29 commented on April 27, 2024 3

Absolutely. I'll work on it this week

from config.

kamilmysliwiec avatar kamilmysliwiec commented on April 27, 2024 2

So basically you're suggesting that we could expose this helper type out of the box, as part of the @nestjs/config package:

export class ConditionalModule {
  static async registerWhen(moduleToRegister, condition) {
    await ConfigModule.envVariablesLoaded();
    return condition(process.env) 
         ? { module: ConditionalModule, imports: [moduleToRegister] 
         : { module: ConditionalModule, imports: [] }
  }
}

Example

imports: [ConditionalModule.registerWhen(ScheduleModule.forRoot(), ({ USE_CRON }) => ...)]

from config.

jmcdo29 avatar jmcdo29 commented on April 27, 2024 1

Hmmm, I'm not quite sure how feasible that would be, as injecting a provider is usually for module configuration, not module registration, y'know? But as imports can have async values, we can make use of the existing method for waiting for process.env to be loaded and return the correct module. I do like the registerModule with the object rather than the two parameters though

from config.

micalevisk avatar micalevisk commented on April 27, 2024

can you show some code snippet on how it would looks like from consumer side?

from config.

jmcdo29 avatar jmcdo29 commented on April 27, 2024
@Module({
  imports: [
    ConfigModule.forRoot(),
    ConfifgModule.optionallyRegisterModule(
      ScheduleModule.forRoot(),
      'USE_CRON'
    )
  ],
  ...
})
export class AppModule {}

OR

@Module({
  imports: [
    ConfigModule.forRoot(),
    ConfifgModule.optionallyRegisterModule(
      ScheduleModule.forRoot(),
      () => JSON.parse(process.env.USE_ENV)
    )
  ],
  ...
})
export class AppModule {}

from config.

micalevisk avatar micalevisk commented on April 27, 2024

I wonder if it couldn't be something that works on top of ConfigService:

@Module({
  imports: [
    ConfigModule.forRoot(),
    ConfigModule.registerModule({
      module: ScheduleModule.forRoot(),
      when: (configService: ConfigService) =>
        configService.get('USE_ENV') == true
    })
  ],
  ...
})
export class AppModule {}

from config.

jmcdo29 avatar jmcdo29 commented on April 27, 2024

@kamilmysliwiec yep, that's generally it. Figured we'd make it a part of this package as it's very closely tied to the module

from config.

kamilmysliwiec avatar kamilmysliwiec commented on April 27, 2024

This would make a lot of sense indeed. Would you like to create a PR for this?

from config.

jmcdo29 avatar jmcdo29 commented on April 27, 2024

Let's track this here

from config.

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.