Created
July 29, 2020 01:50
-
-
Save chochinlu/33f30bba35d06e7864613ea0da56e16d to your computer and use it in GitHub Desktop.
NestJS + TypeORM + env CongfigService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { AppController } from './app.controller'; | |
import { AppService } from './app.service'; | |
import { TasksModule } from './tasks/tasks.module'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
import { typeOrmConfig } from './config/typeorm.config'; | |
import { ConfigModule, ConfigService } from '@nestjs/config'; | |
@Module({ | |
imports: [ | |
ConfigModule.forRoot(), | |
TypeOrmModule.forRootAsync({ | |
imports: [ConfigModule], | |
inject: [ConfigService], | |
useFactory: (configService: ConfigService) => | |
typeOrmConfig(configService), | |
}), | |
TasksModule, | |
], | |
controllers: [AppController], | |
providers: [AppService], | |
}) | |
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TypeOrmModuleOptions } from '@nestjs/typeorm'; | |
import { ConfigService } from '@nestjs/config'; | |
export const typeOrmConfig = ( | |
configService: ConfigService, | |
): TypeOrmModuleOptions => ({ | |
type: 'postgres', | |
host: configService.get<string>('DATABASE_HOST'), | |
port: 5432, | |
username: configService.get<string>('DATABASE_USER'), | |
password: configService.get<string>('DATABASE_PASSWORD'), | |
database: configService.get<string>('DATABASE'), | |
entities: [__dirname + '/../**/*.entity.{js,ts}'], | |
synchronize: true, | |
retryAttempts: 1, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment