db initialize through env flag

This commit is contained in:
Kallesh B S 2025-09-19 12:13:02 +05:30
parent 2a45c0b522
commit c08b8bb19f
4 changed files with 55 additions and 52 deletions

View File

@ -55,7 +55,7 @@ export class AuthController {
@UseInterceptors(LogoutInterceptor) @UseInterceptors(LogoutInterceptor)
@HttpCode(200) @HttpCode(200)
@Post('logout') @Post('1/logout')
async logout(@Req() req: any, @Res() res: Response) { async logout(@Req() req: any, @Res() res: Response) {
const refreshToken = req.user?.refreshToken; const refreshToken = req.user?.refreshToken;
let rst: any = await this.authService.logoutUser(refreshToken); let rst: any = await this.authService.logoutUser(refreshToken);
@ -74,7 +74,7 @@ export class AuthController {
res.json({ ...rst }) res.json({ ...rst })
} }
@Get('refresh-tokens') @Get('1/refresh-tokens')
async getTokenFromRefreshToken(@Req() req: Request, @Res({ passthrough: true }) res: Response) { async getTokenFromRefreshToken(@Req() req: Request, @Res({ passthrough: true }) res: Response) {
let k: any = await this.authService.getTokenFromRefreshToken(req) let k: any = await this.authService.getTokenFromRefreshToken(req)
@ -103,7 +103,7 @@ export class AuthController {
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@Roles('sa') @Roles('sa')
@Post('sendmail') @Post('1/sendmail')
@HttpCode(200) @HttpCode(200)
async sendMail(@Body() body: SendMailDTO) { async sendMail(@Body() body: SendMailDTO) {
// let token = await this.jwtService.generateToken({ email: mail }) // let token = await this.jwtService.generateToken({ email: mail })

View File

@ -12,10 +12,12 @@ export class OracleDBService {
private readonly logger = new Logger(OracleDBService.name); private readonly logger = new Logger(OracleDBService.name);
constructor(private readonly configService: ConfigService) { constructor(private readonly configService: ConfigService) {
this.initializePool().catch(err => { if (process.env.DB_FOR === "ORACLE") {
this.logger.error('Error initializing Oracle DB pool', err); this.initializePool().catch(err => {
throw new InternalServerException(); this.logger.error('Error initializing Oracle DB pool', err);
}); throw new InternalServerException();
});
}
} }
private async initializePool(): Promise<void> { private async initializePool(): Promise<void> {
@ -63,54 +65,19 @@ export class OracleDBService {
} }
} }
@Injectable()
export class MssqlDBService {
private pool: ConnectionPool;
private poolConnected = false;
constructor(private readonly configService: ConfigService) {
const config: any = getMssqlConfig(configService);
this.pool = new ConnectionPool({
...config,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
});
}
async getConnection(): Promise<ConnectionPool> {
try {
if (!this.poolConnected) {
this.pool.on('error', err => {
console.error('SQL Pool Error:', err);
this.poolConnected = false;
});
await this.pool.connect();
this.poolConnected = true;
}
return this.pool;
} catch (error) {
console.error('Failed to get connection from pool:', error);
throw error;
}
}
}
@Injectable() @Injectable()
export class PgDBService implements OnModuleDestroy { export class PgDBService implements OnModuleDestroy {
private pool: pgPool | null = null; private pool: pgPool | null = null;
private readonly logger = new Logger(PgDBService.name); private readonly logger = new Logger(PgDBService.name);
constructor(private readonly configService: ConfigService) { constructor(private readonly configService: ConfigService) {
this.initializePool().catch(err => { if (process.env.DB_FOR === "PG") {
this.logger.error('Error initializing PostgreSQL DB pool', err); this.initializePool().catch(err => {
// Re-throw to indicate a critical startup failure, preventing the application from starting this.logger.error('Error initializing PostgreSQL DB pool', err);
throw new InternalServerException('Failed to initialize database pool'); // Re-throw to indicate a critical startup failure, preventing the application from starting
}); throw new InternalServerException('Failed to initialize database pool');
});
}
} }
/** /**
@ -199,4 +166,40 @@ export class PgDBService implements OnModuleDestroy {
} }
} }
} }
} }
@Injectable()
export class MssqlDBService {
private pool: ConnectionPool;
private poolConnected = false;
constructor(private readonly configService: ConfigService) {
const config: any = getMssqlConfig(configService);
this.pool = new ConnectionPool({
...config,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
});
}
async getConnection(): Promise<ConnectionPool> {
try {
if (!this.poolConnected) {
this.pool.on('error', err => {
console.error('SQL Pool Error:', err);
this.poolConnected = false;
});
await this.pool.connect();
this.poolConnected = true;
}
return this.pool;
} catch (error) {
console.error('Failed to get connection from pool:', error);
throw error;
}
}
}

View File

@ -9,7 +9,7 @@ import { RolesGuard } from 'src/guards/roles.guard';
@ApiTags('Manage Clients - PG') @ApiTags('Manage Clients - PG')
// @UseGuards(JwtAuthGuard, RolesGuard) // @UseGuards(JwtAuthGuard, RolesGuard)
@Roles('sa') @Roles('sa')
@Controller('1') @Controller('2')
export class ManageClientsController { export class ManageClientsController {
constructor(private readonly manageClientsService: ManageClientsService) { } constructor(private readonly manageClientsService: ManageClientsService) { }

View File

@ -10,7 +10,7 @@ import { RolesGuard } from 'src/guards/roles.guard';
@ApiTags('Manage Holders - PG') @ApiTags('Manage Holders - PG')
// @UseGuards(JwtAuthGuard, RolesGuard) // @UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ca', 'sa') @Roles('ca', 'sa')
@Controller('manage-holder') @Controller('2')
export class ManageHolderController { export class ManageHolderController {
constructor(private readonly manageHoldersService: ManageHolderService) { } constructor(private readonly manageHoldersService: ManageHolderService) { }