File size: 1,156 Bytes
3cdf53d
 
 
c114c72
 
 
 
 
 
3cdf53d
 
 
 
 
 
13dd365
3cdf53d
c114c72
3cdf53d
 
 
 
 
 
13dd365
 
 
 
 
3cdf53d
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';

import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

@Injectable()
export class DatabaseConfigService implements TypeOrmOptionsFactory {
  constructor(private readonly configService: ConfigService) {}

  createTypeOrmOptions(): TypeOrmModuleOptions {
    const isSslEnabled = this.configService.get('db.ssl_enabled') !== 'false';
    return {
      type: 'postgres',
      host: this.configService.get('db.host'),
      port: this.configService.get('db.port'),
      username: this.configService.get('db.user_name'),
      database: this.configService.get('db.name'),
      password: this.configService.get('db.password'),
      entities: [path.join(__dirname, '../**/*.entity{.ts,.js}')],
      ssl: isSslEnabled
        ? {
            rejectUnauthorized: false,
          }
        : false,
      maxQueryExecutionTime: this.configService.get('db.slow_limit'),
    };
  }
}