From 9a39c89dd9cebde07be34b6295ec7028ca8184b6 Mon Sep 17 00:00:00 2001 From: wayanrivan Date: Mon, 30 Mar 2026 10:02:49 +0700 Subject: [PATCH] update service fare --- src/helpers/orm.ts | 165 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 126 insertions(+), 39 deletions(-) diff --git a/src/helpers/orm.ts b/src/helpers/orm.ts index 1e005ec..b555f70 100644 --- a/src/helpers/orm.ts +++ b/src/helpers/orm.ts @@ -1,14 +1,84 @@ +// import config from 'config'; +// import { DataSource } from "typeorm"; +// import { ILogObj, Logger } from 'tslog'; +// import { FareType, RoomStock, Administrativu, Aldeia, District, Menu, Munisipo, Suco, Application, Nationality, Diagnosis, DiagnosticProcedure, Room, ServiceType, ServiceClass, Service, ServiceFare, ServicePackage, Department, DoctorSchedule, MeasurementUnit, ReferenceRange, QueueMonitoring, ExaminationDetail, ExaminationType, User, UserRole, HrmsEmployee, HrmsDepartment, HrmsPosition, HrmsShift, HospitalInformation, ItemOrigin, ItemType, ItemTypeDetail, Unit, ItemCategory, ItemStatus, UsageInstructions, UsageTime, ItemClass, ItemClassDetail, GenericName, Factory, Supplier, FactoryToSupplier, ItemMaster, ItemGroup, PharmacyInfo, ItemPrice, SellingPricePercentage, PatientGroup, PatientGuarantor, InitialStock, DoctorItem, DoctorItemPackage, UserToRoom, RoomToPharmacy, PlanningVerificator, ScheduleSeries, ScheduleException, CardType, PaymentMethod } from 'entity' + +// export class OrmHelper { +// static DB: DataSource = null + +// static setup() { +// const log: Logger = new Logger({ name: '[OrmHelper]', type: 'pretty' }); + +// const engine: 'mysql' | 'postgres' = config.get("database.engine") + +// OrmHelper.DB = new DataSource({ +// type: engine, +// host: config.get("database.host"), +// port: Number(config.get("database.port")), +// username: String(config.get("database.username")), +// password: String(config.get("database.password")), +// database: String(config.get("database.database")), +// synchronize: true, +// logging: config.get('database.logging'), +// entities: [Menu, Administrativu, Aldeia, District, Munisipo, Suco, Application, Nationality, Diagnosis, DiagnosticProcedure, Room, ServiceType, ServiceClass, Service, ServiceFare, ServicePackage, Department, DoctorSchedule, MeasurementUnit, ReferenceRange, QueueMonitoring, ExaminationDetail, ExaminationType, User, UserRole, HrmsEmployee, HrmsDepartment, HrmsPosition, HrmsShift, HospitalInformation, ItemGroup, +// ItemOrigin, +// ItemType, +// ItemTypeDetail, +// Unit, +// ItemCategory, +// ItemStatus, +// UsageInstructions, +// UsageTime, +// ItemClass, +// ItemClassDetail, +// GenericName, +// Factory, +// Supplier, +// FactoryToSupplier, +// ItemMaster, +// PharmacyInfo, +// ItemPrice, +// SellingPricePercentage, +// PatientGroup, +// PatientGuarantor, +// InitialStock, +// DoctorItem, +// DoctorItemPackage, +// UserToRoom, +// RoomToPharmacy, +// PlanningVerificator, +// RoomStock, +// ScheduleSeries, +// ScheduleException, +// FareType, +// CardType, +// PaymentMethod +// ], +// subscribers: [], +// migrations: [], +// }) + +// OrmHelper.DB.initialize() +// .then(() => { +// // here you can start to work with your database +// }) +// .catch((error: any) => log.error(error)) +// } +// } + import config from 'config'; import { DataSource } from "typeorm"; import { ILogObj, Logger } from 'tslog'; import { FareType, RoomStock, Administrativu, Aldeia, District, Menu, Munisipo, Suco, Application, Nationality, Diagnosis, DiagnosticProcedure, Room, ServiceType, ServiceClass, Service, ServiceFare, ServicePackage, Department, DoctorSchedule, MeasurementUnit, ReferenceRange, QueueMonitoring, ExaminationDetail, ExaminationType, User, UserRole, HrmsEmployee, HrmsDepartment, HrmsPosition, HrmsShift, HospitalInformation, ItemOrigin, ItemType, ItemTypeDetail, Unit, ItemCategory, ItemStatus, UsageInstructions, UsageTime, ItemClass, ItemClassDetail, GenericName, Factory, Supplier, FactoryToSupplier, ItemMaster, ItemGroup, PharmacyInfo, ItemPrice, SellingPricePercentage, PatientGroup, PatientGuarantor, InitialStock, DoctorItem, DoctorItemPackage, UserToRoom, RoomToPharmacy, PlanningVerificator, ScheduleSeries, ScheduleException, CardType, PaymentMethod } from 'entity' +const log: Logger = new Logger({ name: '[OrmHelper]', type: 'pretty' }); + export class OrmHelper { static DB: DataSource = null + private static retryDelay = 5000 // 5 detik + private static maxRetries = 10 static setup() { - const log: Logger = new Logger({ name: '[OrmHelper]', type: 'pretty' }); - const engine: 'mysql' | 'postgres' = config.get("database.engine") OrmHelper.DB = new DataSource({ @@ -20,48 +90,65 @@ export class OrmHelper { database: String(config.get("database.database")), synchronize: true, logging: config.get('database.logging'), + // Tambahan config pool + extra: { + max: 20, + min: 2, + idleTimeoutMillis: 30000, + connectionTimeoutMillis: 5000, + }, entities: [Menu, Administrativu, Aldeia, District, Munisipo, Suco, Application, Nationality, Diagnosis, DiagnosticProcedure, Room, ServiceType, ServiceClass, Service, ServiceFare, ServicePackage, Department, DoctorSchedule, MeasurementUnit, ReferenceRange, QueueMonitoring, ExaminationDetail, ExaminationType, User, UserRole, HrmsEmployee, HrmsDepartment, HrmsPosition, HrmsShift, HospitalInformation, ItemGroup, - ItemOrigin, - ItemType, - ItemTypeDetail, - Unit, - ItemCategory, - ItemStatus, - UsageInstructions, - UsageTime, - ItemClass, - ItemClassDetail, - GenericName, - Factory, - Supplier, - FactoryToSupplier, - ItemMaster, - PharmacyInfo, - ItemPrice, - SellingPricePercentage, - PatientGroup, - PatientGuarantor, - InitialStock, - DoctorItem, - DoctorItemPackage, - UserToRoom, - RoomToPharmacy, - PlanningVerificator, - RoomStock, - ScheduleSeries, - ScheduleException, - FareType, - CardType, - PaymentMethod + ItemOrigin, ItemType, ItemTypeDetail, Unit, ItemCategory, ItemStatus, UsageInstructions, UsageTime, ItemClass, ItemClassDetail, GenericName, Factory, Supplier, FactoryToSupplier, ItemMaster, PharmacyInfo, ItemPrice, SellingPricePercentage, PatientGroup, PatientGuarantor, InitialStock, DoctorItem, DoctorItemPackage, UserToRoom, RoomToPharmacy, PlanningVerificator, RoomStock, ScheduleSeries, ScheduleException, FareType, CardType, PaymentMethod ], subscribers: [], migrations: [], }) - OrmHelper.DB.initialize() - .then(() => { - // here you can start to work with your database - }) - .catch((error: any) => log.error(error)) + OrmHelper.initialize() + } + + static async initialize(retryCount = 0) { + try { + // Kalau sebelumnya sudah connect, destroy dulu + if (OrmHelper.DB.isInitialized) { + await OrmHelper.DB.destroy() + } + + await OrmHelper.DB.initialize() + log.info("Database connected successfully") + + // Monitor koneksi — reconnect kalau terputus + OrmHelper.monitorConnection() + + } catch (error: any) { + log.error(`Database connection failed (attempt ${retryCount + 1}):`, error.message) + + if (retryCount < OrmHelper.maxRetries) { + log.info(`Retrying in ${OrmHelper.retryDelay / 1000}s...`) + setTimeout(() => OrmHelper.initialize(retryCount + 1), OrmHelper.retryDelay) + } else { + log.error("Max retries reached. Could not connect to database.") + } + } + } + + static monitorConnection() { + // Cek koneksi setiap 30 detik + setInterval(async () => { + try { + if (!OrmHelper.DB.isInitialized) { + log.warn("Database connection lost, reconnecting...") + await OrmHelper.initialize() + return + } + + // Ping DB untuk validasi koneksi masih hidup + await OrmHelper.DB.query("SELECT 1") + + } catch (error: any) { + log.warn("Database ping failed, reconnecting...", error.message) + await OrmHelper.initialize() + } + }, 30000) // setiap 30 detik } } \ No newline at end of file