SpaceNinjaServer/src/services/mongoService.ts

23 lines
626 B
TypeScript
Raw Normal View History

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
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 () => {
try {
2024-01-06 16:26:58 +01:00
await mongoose.connect(`${url}`);
logger.info("connected to MongoDB");
} 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-19 15:22:48 -03:00
}
};
export { connectDatabase };