Source

database/index.js

import { getLogger } from "@utils/asyncLocalStorage";
import { connectToDatabase, closeDatabaseConnection } from "./mongo.connection";
import { connectToRedis, closeRedisConnection } from "./redis.client";
/**
 * Initialize database connections.
 * @category Database
 * @returns {Promise<void>} Resolves when MongoDB and Redis are ready.
 * @throws {unknown} Propagates connection errors.
 */
export async function initializeDatabases() {
    const logger = getLogger();
    logger.info("Initializing database connections...");
    try {
        await connectToDatabase();
        connectToRedis();
        logger.info("All database connections initialized successfully");
    }
    catch (error) {
        logger.error({
            error: error instanceof Error ? error.message : "Unknown error",
        }, "Failed to initialize database connections");
        throw error;
    }
}
/**
 * Close database connections.
 * @category Database
 * @returns {Promise<void>} Resolves when MongoDB and Redis connections are closed.
 * @throws {unknown} Propagates shutdown errors.
 */
export async function closeDatabases() {
    const logger = getLogger();
    logger.info("Closing database connections...");
    try {
        await closeRedisConnection();
        await closeDatabaseConnection();
        logger.info("All database connections closed successfully");
    }
    catch (error) {
        logger.error({
            error: error instanceof Error ? error.message : "Unknown error",
        }, "Error closing database connections");
        throw error;
    }
}