Skip to content

Implement forRootAsync to allow dynamic configuration injection #2649

@drew-r

Description

@drew-r

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

Currently, TerminusModule only supports forRoot(), which requires static configuration values at the time of module import. This makes it difficult to inject dynamic configurations, such as gracefulShutdownTimeoutMs, from ConfigService or other asynchronous providers.

For example, we want to configure gracefulShutdownTimeoutMs dynamically based on an environment variable managed by ConfigService:

import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule,
    TerminusModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        gracefulShutdownTimeoutMs: configService.get<number>('TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT', 16000),
      }),
    }),
  ],
})
export class HealthModule {}

Describe the solution you'd like

A forRootAsync impl that can set TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT + the other logging config values

Teachability, documentation, adoption, migration strategy

n/a

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

Having to work around like so

const terminusModule = TerminusModule.forRoot();
terminusModule.providers?.push({
  provide: TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT,
  useFactory: (configService: ConfigService) => configService.getOrThrow<number>('TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT'),
  inject: [ConfigService]
});
terminusModule.providers?.push(GracefulShutdownService);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions