Spaces:
Paused
Paused
| import mongoose from 'mongoose'; | |
| import { MongoClient } from 'mongodb'; | |
| import { loadEnv } from '../utils/general'; | |
| const mongoURI: string | undefined = loadEnv('MONGO_URI'); | |
| if (!mongoURI) { | |
| throw new Error("MONGO_URI is not defined in the environment variables."); | |
| } | |
| export const mongoClient = new MongoClient(mongoURI); | |
| export const connectDB = async () => { | |
| if (!mongoURI) { | |
| console.error('MONGO_URI is not defined'); | |
| process.exit(1); | |
| } | |
| try { | |
| await mongoClient.connect(); | |
| await mongoose.connect(mongoURI); | |
| console.log('MongoDB connected successfully'); | |
| } catch (error) { | |
| console.error('MongoDB connection error:', error); | |
| connectDB(); | |
| } | |
| }; |