Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS Using ConfigService with TypeOrmModule

Tags:

nestjs

I set up a ConfigService as described in docs https://docs.nestjs.com/techniques/configuration

How can I use this service with the the TypeOrmModule?

TypeOrmModule.forRoot({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'root',
  database: 'test',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true,
}),
like image 701
Brucie Alpha Avatar asked Sep 29 '18 16:09

Brucie Alpha


1 Answers

See https://docs.nestjs.com/techniques/database 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 {}
like image 54
Kamil Myśliwiec Avatar answered Oct 11 '22 13:10

Kamil Myśliwiec