Skip to content

Instantly share code, notes, and snippets.

@chochinlu
Created July 29, 2020 01:50
Show Gist options
  • Save chochinlu/33f30bba35d06e7864613ea0da56e16d to your computer and use it in GitHub Desktop.
Save chochinlu/33f30bba35d06e7864613ea0da56e16d to your computer and use it in GitHub Desktop.
NestJS + TypeORM + env CongfigService
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 {}
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