Skip to content

Commit cd5a099

Browse files
Hotfix/db (#100)
* fix(db): fix db config file * fix(db): load ssl config only for digital ocean db
1 parent 08775ba commit cd5a099

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/databases/data-source.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { TweetCategory } from '../tweets/entities/tweet-category.entity';
1515
import { Chat } from '../chat/entities/chat.entity';
1616
import { Message } from '../messages/entities/message.entity';
1717
import { readFileSync } from 'fs';
18+
1819
config({ path: resolve(__dirname, '../../config/.env') });
20+
1921
const config_service = new ConfigService();
2022

21-
export default new DataSource({
23+
const base_config: any = {
2224
type: 'postgres',
2325
host: process.env.POSTGRES_HOST || config_service.get<string>('POSTGRES_HOST'),
2426
username: process.env.POSTGRES_USERNAME || config_service.get<string>('POSTGRES_USERNAME'),
@@ -28,6 +30,7 @@ export default new DataSource({
2830
parseInt(process.env.POSTGRES_PORT || '5432') ||
2931
config_service.get<number>('POSTGRES_PORT') ||
3032
5432,
33+
3134
entities: [
3235
User,
3336
Verification,
@@ -48,10 +51,16 @@ export default new DataSource({
4851
Chat,
4952
Message,
5053
],
51-
ssl: {
52-
ca: readFileSync(process.env.DATABASE_CA!).toString(), // Path to the CA certificate
53-
},
54+
5455
migrations: ['src/migrations/*{.ts,.js}'],
5556
synchronize: false,
5657
uuidExtension: 'pgcrypto',
57-
});
58+
};
59+
60+
if (process.env.DATABASE_CA) {
61+
base_config.ssl = {
62+
ca: readFileSync(process.env.DATABASE_CA).toString(),
63+
};
64+
}
65+
66+
export default new DataSource(base_config);

src/databases/postgresql.module.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
import { Module } from '@nestjs/common';
22
import { ConfigModule, ConfigService } from '@nestjs/config';
33
import { TypeOrmModule } from '@nestjs/typeorm';
4-
import { readFileSync, writeFileSync } from 'fs';
4+
import { existsSync, readFileSync } from 'fs';
55

66
@Module({
77
imports: [
88
ConfigModule,
99
TypeOrmModule.forRootAsync({
1010
imports: [ConfigModule],
1111
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+
},
2739
}),
2840
],
2941
providers: [ConfigService],

0 commit comments

Comments
 (0)