What is custom provider syntax in NestJS (useClass, useValue, useFactory)?
Answer
NestJS supports advanced provider definitions beyond the simple class syntax. useClass: swap implementation based on environment: { provide: ConfigService, useClass: process.env.NODE_ENV === 'test' ? MockConfigService : ConfigService }. useValue: provide a static value or mock: { provide: 'APP_VERSION', useValue: '1.0.0' }. Inject with @Inject('APP_VERSION') version: string. useFactory: create providers with async initialization or dependencies: { provide: 'DB_CONNECTION', useFactory: async (config: ConfigService) => { return await createConnection(config.get('DB_URL')); }, inject: [ConfigService] }. useExisting: alias one provider to another. These patterns enable sophisticated DI configurations for testing, feature flags, and platform-agnostic code.
Previous
How does NestJS handle graceful shutdown?
Next
What is the difference between forRoot() and forFeature() in NestJS modules?
More NestJS Questions
View all →- Advanced What is the CQRS pattern and how does NestJS support it?
- Advanced How do you implement gRPC in NestJS?
- Advanced What is the NestJS Interceptor execution order and how do multiple interceptors interact?
- Advanced How does NestJS handle graceful shutdown?
- Advanced What is the difference between forRoot() and forFeature() in NestJS modules?