SpaceNinjaServer/src/services/mongoService.ts

24 lines
578 B
TypeScript
Raw Normal View History

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;
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 {
await mongoose.connect(url);
console.log("connected to MongoDB");
} catch (error: unknown) {
if (error instanceof Error) {
console.error("error connecting to MongoDB", error.message);
}
2023-05-19 15:22:48 -03:00
}
};
export { connectDatabase };