Refactor DB initialization code

Sépare la configuration de la base de données et l'initialisation des clients en deux méthodes distinctes. Cela améliore la lisibilité et facilite la gestion future des configurations et des clients.
This commit is contained in:
Mathis H (Avnyr) 2024-08-21 15:08:31 +02:00
parent 419aa197a1
commit 76933ca39f
No known key found for this signature in database
GPG Key ID: FF69BF8BF95CDD58

View File

@ -12,7 +12,12 @@ export class DbService {
constructor(private configService: ConfigService) {} constructor(private configService: ConfigService) {}
async onModuleInit() { async onModuleInit() {
const dbConfig = { const dbConfig = this.getDbConfig();
this.initializeClients(dbConfig);
}
private getDbConfig() {
return {
host: this.configService.get<string>("POSTGRES_HOST"), host: this.configService.get<string>("POSTGRES_HOST"),
port: Number(this.configService.get<string>("POSTGRES_PORT")), port: Number(this.configService.get<string>("POSTGRES_PORT")),
database: this.configService.get<string>("POSTGRES_DATABASE"), database: this.configService.get<string>("POSTGRES_DATABASE"),
@ -20,18 +25,18 @@ export class DbService {
password: this.configService.get<string>("POSTGRES_PASSWORD"), password: this.configService.get<string>("POSTGRES_PASSWORD"),
ssl: Boolean(this.configService.get<string>("POSTGRES_SSL")), ssl: Boolean(this.configService.get<string>("POSTGRES_SSL")),
}; };
}
private initializeClients(dbConfig: any) {
this.migrationClient = postgres({ this.migrationClient = postgres({
...dbConfig, ...dbConfig,
max: 1, max: 1,
}); });
this.standardClient = postgres({ this.standardClient = postgres({
...dbConfig, ...dbConfig,
}); });
} }
//onModuleDestroy
async onModuleDestroy() { async onModuleDestroy() {
await this.migrationClient.end(); await this.migrationClient.end();
await this.standardClient.end(); await this.standardClient.end();