added mognodb database

This commit is contained in:
2025-01-30 20:15:50 +01:00
parent c913661109
commit 8fd4ed237b
11 changed files with 326 additions and 7 deletions
+30
View File
@@ -0,0 +1,30 @@
const { MongoClient, ServerApiVersion } = require('mongodb');
const { mongodb } = require("./config.json");
let dbClient;
async function connectToDatabase() {
if (!dbClient) {
// Create a new MongoClient only once
dbClient = new MongoClient(mongodb, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
await dbClient.connect();
console.log("Connected to MongoDB!");
await dbClient.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
}
// Return the existing connection if already connected
return dbClient;
}
module.exports = {
connectToDatabase
};