|
1 | 1 | import { Module } from '@nestjs/common'; |
2 | 2 | import { ConfigModule, ConfigService } from '@nestjs/config'; |
3 | 3 | import { TypeOrmModule } from '@nestjs/typeorm'; |
4 | | -import { readFileSync, writeFileSync } from 'fs'; |
| 4 | +import { existsSync, readFileSync } from 'fs'; |
5 | 5 |
|
6 | 6 | @Module({ |
7 | 7 | imports: [ |
8 | 8 | ConfigModule, |
9 | 9 | TypeOrmModule.forRootAsync({ |
10 | 10 | imports: [ConfigModule], |
11 | 11 | inject: [ConfigService], |
12 | | - useFactory: (config_service: ConfigService) => ({ |
13 | | - type: 'postgres', |
14 | | - host: config_service.get<string>('POSTGRES_HOST'), |
15 | | - username: config_service.get<string>('POSTGRES_USERNAME'), |
16 | | - password: config_service.get<string>('POSTGRES_PASSWORD'), |
17 | | - database: config_service.get<string>('POSTGRES_DB'), |
18 | | - port: config_service.get<number>('POSTGRES_PORT'), |
19 | | - synchronize: false, // Using migrations instead |
20 | | - autoLoadEntities: true, |
21 | | - // logging: ['query'], |
22 | | - // logger: 'advanced-console', |
23 | | - ssl: { |
24 | | - ca: readFileSync(process.env.DATABASE_CA!).toString(), // Path to the CA certificate |
25 | | - }, |
26 | | - }), |
| 12 | + useFactory: (config_service: ConfigService) => { |
| 13 | + // SSL CONFIG |
| 14 | + let ssl: any; |
| 15 | + |
| 16 | + const ca_path = process.env.DATABASE_CA; |
| 17 | + |
| 18 | + if (ca_path) { |
| 19 | + if (existsSync(ca_path)) { |
| 20 | + ssl = { |
| 21 | + ca: readFileSync(ca_path).toString(), |
| 22 | + }; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return { |
| 27 | + type: 'postgres', |
| 28 | + host: config_service.get<string>('POSTGRES_HOST'), |
| 29 | + username: config_service.get<string>('POSTGRES_USERNAME'), |
| 30 | + password: config_service.get<string>('POSTGRES_PASSWORD'), |
| 31 | + database: config_service.get<string>('POSTGRES_DB'), |
| 32 | + port: config_service.get<number>('POSTGRES_PORT'), |
| 33 | + synchronize: false, // Using migrations instead |
| 34 | + autoLoadEntities: true, |
| 35 | + |
| 36 | + ssl, |
| 37 | + }; |
| 38 | + }, |
27 | 39 | }), |
28 | 40 | ], |
29 | 41 | providers: [ConfigService], |
|
0 commit comments