28 lines
997 B
JavaScript
28 lines
997 B
JavaScript
const mongoose = require('mongoose');
|
|
const path = require('path');
|
|
require('dotenv').config({ path: path.join(__dirname, '../../.env.local') });
|
|
|
|
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/backone_dpi';
|
|
|
|
async function connectDB() {
|
|
try {
|
|
if (mongoose.connection.readyState >= 1) return;
|
|
await mongoose.connect(MONGODB_URI, { serverSelectionTimeoutMS: 2000 });
|
|
console.log('[MongoDB] Connected successfully');
|
|
} catch (error) {
|
|
console.error('[MongoDB] Connection failed, switching to In-Memory DB:', error.message);
|
|
try {
|
|
const { MongoMemoryServer } = require('mongodb-memory-server');
|
|
const mongod = await MongoMemoryServer.create();
|
|
const uri = mongod.getUri();
|
|
await mongoose.connect(uri, {});
|
|
console.log('[MongoDB] Connected successfully to Virtual Memory Database!');
|
|
} catch (memErr) {
|
|
console.error('[MongoDB] Memory DB failed:', memErr.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = connectDB;
|
|
|