Source: config/db.js

/**
 * @file Database connection utility.
 * @module config/db
 * @description Handles connecting to MongoDB using Mongoose.
 * @requires mongoose
 * @requires dotenv
 */

const dotenv = require("dotenv");
dotenv.config();

const mongoose = require("mongoose");

/**
 * Establishes a connection to the MongoDB database using Mongoose.
 *
 * - Retrieves the connection URI from the `MONGO_URI` environment variable.
 * - Logs a success message when connected.
 * - Logs the error and exits the process on failure.
 *
 * @async
 * @function connectDB
 * @returns {Promise<void>} Resolves when the connection is successfully established.
 */
const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI);
    console.log(`MongoDB Connected: ${conn.connection.host}`);
  } catch (error) {
    console.error(`Error connecting to MongoDB: ${error.message}`);
    process.exit(1);
  }
};

module.exports = connectDB;