import { TypeOrmModuleOptions } from '@nestjs/typeorm' ; import * as path from 'path' ; require ( 'dotenv' ) . config ( { path : path . resolve ( process . cwd ( ) , 'apps/bootcamp-api/.env' ) } ) ; class ConfigService { constructor ( private env ) { } private getValue ( key , throwOnMissing = true ) { const value = this . env [ key ] ; if ( ! value && throwOnMissing ) { throw new Error ( `config error - missing env.${ key }` ) ; } return value ; } public ensureValues ( keys ) { keys . forEach ( k => this . getValue ( k , true ) ) ; return this ; } public getPort ( ) { return this . getValue ( 'PORT' , true ) ; } public isProduction ( ) { const mode = this . getValue ( 'MODE' , false ) ; return mode != 'DEV' ; } public getTypeOrmConfig ( ) { return { type : 'postgres' , host : this . getValue ( 'POSTGRES_HOST' ) , port : parseInt ( this . getValue ( 'POSTGRES_PORT' ) ) , username : this . getValue ( 'POSTGRES_USER' ) , password : this . getValue ( 'POSTGRES_PASSWORD' ) , database : this . getValue ( 'POSTGRES_DATABASE' ) , entities : [ '**/*.entity{.ts,.js}' ] , migrationsTableName : 'migration' , migrations : [ 'apps/bootcamp-api/src/migration/*.ts' ] , cli : { migrationsDir : 'apps/bootcamp-api/src/migration' , } , ssl : this . isProduction ( ) , } ; } } const configService = new ConfigService ( process . env ) . ensureValues ( [ 'POSTGRES_HOST' , 'POSTGRES_PORT' , 'POSTGRES_USER' , 'POSTGRES_PASSWORD' , 'POSTGRES_DATABASE' ] ) ; export { configService } ;