brand marke
brand marke

Reputation: 459

How to pass configuration into forRoot

How can I pass configuration object into forRoot() in AppModule? Is there a way to access configService if there is not constructor in the module?

config.yml:

smtp:
  host: 'smtp.host.com'
  port: 10
  secure: true
  auth:
    user: 'username'
    auth: 'password'
@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MailModule.forRoot(), // I want to pass configuration here
  ],
})
export class AppModule {}

Upvotes: 0

Views: 704

Answers (2)

brand marke
brand marke

Reputation: 459

I installed config package and used it like so:

import * as config from 'config';

const smtpConfig = config.get('smtp');

@Module({
  imports: [
    MailModule.forRoot(smtpConfig),
  ],
})
export class AppModule {}

Upvotes: 0

Jay McDoniel
Jay McDoniel

Reputation: 70450

Usually you'd need an async registration method like forRootAsync, but it's up to the module creator to provide this method. If this method does exist you can use a factory and inject to create a class-like provider system that Nest can inject with as shown in the database docs. Without knowing which mailing module you're working with, I can't say more if it supports this or not.

Without that async registration method, it's not possible to use DI in the forRoot.

Upvotes: 1

Related Questions