Skip to content

Add a forRootAsync method to dynamically configure module settings (e.g. using the ConfigService) #2121

@daanhegger

Description

@daanhegger

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

When configuring the ScheduleModule settings, like the cronJobs param, I would like to use a value of my ConfigService. With the current forRoot method, it is not possible to access an instance of the ConfigService.

Describe the solution you'd like

Other (native) NestJS modules provide a method forRootAsync, which allows importing and injecting the ConfigService to consume its values for setting up the module.

Teachability, documentation, adoption, migration strategy

Example usage:

ScheduleModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (configService: ConfigService<AppConfig>) => ({
    cronJobs: configService.get('ENABLE_CRON_JOBS', { infer: true }),
  }),
}),

What is the motivation / use case for changing the behavior?

Implementing/fixing this

  • Allows for a cleaner module setup using a single source of truth.
  • Aligns the API with many other NestJS modules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions