-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathmongodb.js
41 lines (35 loc) · 1.41 KB
/
mongodb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { MongoClient } = require('mongodb');
const colors = require('./UI/colors/colors');
const config = require("./config.js");
require('dotenv').config();
let client;
if (config.mongodbUri) {
const uri = config.mongodbUri;
client = new MongoClient(uri);
} else {
console.warn("\x1b[33m[ WARNING ]\x1b[0m MongoDB URI is not defined in the configuration.");
}
async function connectToDatabase() {
if (!client) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m Skipping MongoDB connection as URI is not provided.");
return;
}
try {
await client.connect();
console.log('\n' + '─'.repeat(40));
console.log(`${colors.magenta}${colors.bright}🕸️ DATABASE CONNECTION${colors.reset}`);
console.log('─'.repeat(40));
console.log('\x1b[36m[ DATABASE ]\x1b[0m', '\x1b[32mConnected to MongoDB ✅\x1b[0m');
} catch (err) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m Could not connect to MongoDB. Continuing without database functionality.");
console.error(err.message); // Optional: log detailed error message
}
}
const db = client ? client.db("PrimeMusicSSRR") : null;
const playlistCollection = db ? db.collection("SongPlayLists") : null;
const autoplayCollection = db ? db.collection("AutoplaySettings") : null;
module.exports = {
connectToDatabase,
playlistCollection,
autoplayCollection,
};