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