1- import { registerAs } from '@nestjs/config' ;
1+ import { ConfigService } from '@nestjs/config' ;
22
33import { config } from 'dotenv' ;
44import { join } from 'path' ;
@@ -11,29 +11,33 @@ config({
1111 path : getEnvPath ( cwd ( ) ) ,
1212} ) ;
1313
14- const typeormConfig : DataSourceOptions = {
15- type : 'postgres' ,
16- host : env . DB_HOST ,
17- port : Number ( env . DB_PORT ) ,
18- username : env . DB_USER ,
19- password : env . DB_PASSWORD ,
20- database : env . DB_NAME ,
21- entities :
22- env . NODE_ENV === 'test'
23- ? [ join ( cwd ( ) , 'src' , '**' , '*.entity.{ts,js}' ) ]
24- : [ join ( cwd ( ) , 'dist' , '**' , '*.entity.js' ) ] ,
25- synchronize : env . NODE_ENV !== 'production' ,
26- dropSchema : env . NODE_ENV === 'test' ,
27- migrations : [
28- join ( cwd ( ) , 'dist' , 'common' , 'database' , 'migrations' , '*{.ts,.js}' ) ,
29- ] ,
30- migrationsRun : false ,
31- logging : false ,
14+ export const setTypeormConfig = (
15+ conf : NodeJS . ProcessEnv | ConfigService ,
16+ ) : DataSourceOptions => {
17+ const getConfigValue =
18+ conf instanceof ConfigService
19+ ? conf . get . bind ( conf )
20+ : ( key : string ) => conf [ key ] ;
21+
22+ return {
23+ type : 'postgres' ,
24+ host : getConfigValue ( 'DB_HOST' ) ,
25+ port : Number ( getConfigValue ( 'DB_PORT' ) ) ,
26+ username : getConfigValue ( 'DB_USER' ) ,
27+ password : getConfigValue ( 'DB_PASSWORD' ) ,
28+ database : getConfigValue ( 'DB_NAME' ) ,
29+ entities :
30+ getConfigValue ( 'NODE_ENV' ) === 'test'
31+ ? [ join ( cwd ( ) , 'src' , '**' , '*.entity.{ts,js}' ) ]
32+ : [ join ( cwd ( ) , 'dist' , '**' , '*.entity.js' ) ] ,
33+ synchronize : getConfigValue ( 'NODE_ENV' ) !== 'production' ,
34+ dropSchema : getConfigValue ( 'NODE_ENV' ) === 'test' ,
35+ migrations : [
36+ join ( cwd ( ) , 'dist' , 'common' , 'database' , 'migrations' , '*{.ts,.js}' ) ,
37+ ] ,
38+ migrationsRun : false ,
39+ logging : false ,
40+ } ;
3241} ;
3342
34- export const typeormConfigKey = 'typeorm' ;
35- export const typeormConfigLoader = registerAs (
36- typeormConfigKey ,
37- ( ) => typeormConfig ,
38- ) ;
39- export default new DataSource ( typeormConfig ) ;
43+ export default new DataSource ( setTypeormConfig ( env ) ) ;
0 commit comments