NestJS Using ConfigService with TypeOrmModule

If you'd like to use the configuration class ConfigService, use useClass and provide TypeOrmConfigService

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from './config/config/config.module';
import { TypeOrmConfigService } from './config/typeorm.config';
import { ConfigService } from './config/config/config.service';

@Module({
  imports: [
    ConfigModule,
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useClass: TypeOrmConfigService,
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

TypeORM Integration doc: https://docs.nestjs.com/techniques/database#async-configuration

Configuration: https://docs.nestjs.com/techniques/configuration#using-the-configservice


See https://docs.nestjs.com/techniques/database#async-configuration Async Configuration chapter

import { ConfigService } from './config.service'
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (config: ConfigService) => config.get('database'),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

Tags:

Nestjs