Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read config value using ConfigModule inside of a provider?

I would like to add a config module to my nestjs application and use some config in my database module.

For example I would like to get the host value of my configuration:
In a class I can do const dbHost = this.configService.get<string>('database.host');, but in my code I need the value inside of a provider. How do I get config values inside of a provider?

app.module.ts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@Module({
  imports: [
    DatabaseModule,
    ConfigModule.forRoot({
      load: [configuration],
    }),
  ],
})
export class AppModule {}

database.module.ts

import { Module, Inject } from '@nestjs/common'
import { MongoClient, MongoClientOptions, Db, Logger } from 'mongodb'

@Module({
  providers: [
    {
      provide: 'DATABASE_CLIENT',
      useFactory: () => ({ client: null })
    },
    {
      provide: 'DATABASE_CONNECTION',
      inject: ['DATABASE_CLIENT'],
      useFactory: async (dbClient): Promise<Db> => {
        Logger.setLevel('debug')

        // How do I get the host value of my config?

        const mongo: string = 'mongodb://127.0.0.1:27017'
        const database: string = 'data'
        const options: MongoClientOptions = {}
        const client = new MongoClient(mongo, options)
        await client.connect()
        const db = client.db(database)
        return db
      }
    }
  ],
  exports: ['DATABASE_CONNECTION', 'DATABASE_CLIENT'],
  import: [ConfigModule]
})
export class DatabaseModule {
  constructor(@Inject('DATABASE_CLIENT') private dbClient) {}

  onApplicationShutdown(signal: string) {
    if (signal) console.log(signal + ' signal recieved')
  }
}

>Solution :

In your inject add ConfigService, then it’ll be the second value of useFactory‘s parameters.

@Module({
  providers: [
    {
      provide: 'DATABASE_CLIENT',
      useFactory: () => ({ client: null })
    },
    {
      provide: 'DATABASE_CONNECTION',
      inject: ['DATABASE_CLIENT', ConfigService],
      useFactory: async (dbClient, config: ConfigService): Promise<Db> => {
        Logger.setLevel('debug')

       const dbHost = config.get('database.host')

        const mongo: string = 'mongodb://127.0.0.1:27017'
        const database: string = 'data'
        const options: MongoClientOptions = {}
        const client = new MongoClient(mongo, options)
        await client.connect()
        const db = client.db(database)
        return db
      }
    }
  ],
  exports: ['DATABASE_CONNECTION', 'DATABASE_CLIENT'],
  import: [ConfigModule]
})
export class DatabaseModule {
  constructor(@Inject('DATABASE_CLIENT') private dbClient) {}

  onApplicationShutdown(signal: string) {
    if (signal) console.log(signal + ' signal recieved')
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading