Files
librechat.ai/utils/dbConnect.js
2024-05-09 17:09:13 -04:00

26 lines
659 B
JavaScript

/* eslint-disable no-undef */
import mongoose from 'mongoose'
async function dbConnect() {
try {
if (mongoose.connection.readyState >= 1) {
console.debug('MongoDB connection already established.')
return
}
console.debug('Connecting to MongoDB...')
mongoose.set('debug', true)
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
console.debug('MongoDB connection successful.')
} catch (error) {
console.error('MongoDB connection error:', error)
throw error // Rethrow the error to handle it in the calling code
}
}
export default dbConnect