2024-01-06 16:26:58 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2024-05-15 21:55:59 +02:00
|
|
|
import { config } from "@/src/services/configService";
|
2023-05-19 15:22:48 -03:00
|
|
|
import mongoose from "mongoose";
|
|
|
|
|
2024-05-15 21:55:59 +02:00
|
|
|
const url = config.mongodbUrl;
|
2023-05-19 15:22:48 -03:00
|
|
|
|
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
|
|
|
|
2024-12-23 22:48:16 +01:00
|
|
|
export const connectDatabase = async (): Promise<void> => {
|
2023-05-23 20:42:06 -04:00
|
|
|
try {
|
2024-01-06 16:26:58 +01:00
|
|
|
await mongoose.connect(`${url}`);
|
2024-05-30 13:33:49 +02:00
|
|
|
logger.info("Connected to MongoDB");
|
2023-05-23 20:42:06 -04:00
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
2024-05-30 13:33:49 +02: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
|
|
|
}
|
|
|
|
};
|