small error handling for database.service.ts and refactoring

This commit is contained in:
StarAppeal
2025-09-19 22:31:21 +02:00
parent 85afec219c
commit 1d97b00811
+40 -5
View File
@@ -2,10 +2,45 @@ import "dotenv/config";
import mongoose from "mongoose";
export async function connectToDatabase() {
await mongoose.connect(process.env.DB_CONN_STRING!, {
dbName: process.env.DB_NAME!,
});
let isConnected = false;
export async function connectToDatabase() {
if (isConnected) {
console.log("Already connected to MongoDB.");
return;
}
const dbConnString = process.env.DB_CONN_STRING;
const dbName = process.env.DB_NAME;
if (!dbConnString) {
throw new Error("Missing environment variable: DB_CONN_STRING is required for database connection.");
}
if (!dbName) {
throw new Error("Missing environment variable: DB_NAME is required for database connection.");
}
try {
await mongoose.connect(dbConnString, {
dbName: dbName,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
family: 4
});
isConnected = true;
console.log("Connected to MongoDB with Mongoose");
mongoose.connection.on('disconnected', () => {
// TODO: add reconnecting
console.warn('Mongoose disconnected from DB. Attempting to reconnect...');
isConnected = false;
});
mongoose.connection.on('error', (err) => {
console.error('Mongoose connection error:', err);
isConnected = false;
});
} catch (error) {
console.error("Failed to connect to MongoDB:", error);
process.exit(1);
}
console.log("Connected to MongoDB with Mongoose");
}