2024-01-06 16:26:58 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2023-05-19 15:22:48 -03:00
|
|
|
import * as dotenv from "dotenv";
|
|
|
|
import mongoose from "mongoose";
|
|
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
const url = process.env.MONGODB_URL;
|
|
|
|
|
2023-06-02 11:35:14 -03:00
|
|
|
if (url === undefined) {
|
|
|
|
throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!");
|
|
|
|
}
|
2023-05-19 15:22:48 -03:00
|
|
|
|
|
|
|
const connectDatabase = async () => {
|
2023-05-23 20:42:06 -04:00
|
|
|
try {
|
2024-01-06 16:26:58 +01:00
|
|
|
await mongoose.connect(`${url}`);
|
|
|
|
logger.info("connected to MongoDB");
|
2023-05-23 20:42:06 -04:00
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.error(`error connecting to MongoDB ${error.message}`);
|
2023-05-23 20:42:06 -04:00
|
|
|
}
|
2023-05-19 15:22:48 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export { connectDatabase };
|