// Import necessary modules import { MongoClient } from "mongodb"; import { DebugBuilder } from "../debugger.mjs"; const log = new DebugBuilder("server", "mongoHandler"); import dotenv from "dotenv"; dotenv.config(); // MongoDB connection URI const uri = process.env.MONGO_URL; // Function to connect to the database export const connectToDatabase = async () => { try { const client = await MongoClient.connect(uri); return client; } catch (error) { console.error("Error connecting to the database:", error); throw error; } }; // Function to insert a document into the collection export const insertDocument = async (collectionName, document) => { const db = await connectToDatabase(); log.DEBUG("Inserting document:", collectionName, document); try { const collection = db.db().collection(collectionName); const result = await collection.insertOne(document); log.DEBUG("Document inserted:", result.insertedId); return result.insertedId; } catch (error) { console.error("Error inserting document:", error); throw error; } finally { // Close the connection await db.close(); } }; // Function to retrieve documents from the collection export const getDocuments = async (collectionName) => { log.DEBUG("Getting all documents:", collectionName); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); const documents = await collection.find({}).toArray(); log.DEBUG("Documents retrieved:", documents); return documents; } catch (error) { console.error("Error retrieving documents:", error); throw error; } finally { // Close the connection await db.close(); } }; // Function to retrieve a document by a specific field export const getDocumentByField = async (collectionName, field, value) => { log.DEBUG("Getting document by field:", collectionName, field, value); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); const document = await collection.findOne({ [field]: value }); return document; } catch (error) { console.error("Error retrieving document:", error); throw error; } finally { await db.close(); } }; // Function to update a document by a specific field export const updateDocumentByField = async ( collectionName, field, value, updatedFields, ) => { log.DEBUG( "Update document by field:", collectionName, field, value, updatedFields, ); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); const result = await collection.updateOne( { [field]: value }, { $set: updatedFields }, ); log.DEBUG("Document updated:", result.modifiedCount); return result.modifiedCount; } catch (error) { console.error("Error updating document:", error); throw error; } finally { await db.close(); } }; // Function to delete a document by a specific field export const deleteDocumentByField = async (collectionName, field, value) => { log.DEBUG("Delete document by field:", collectionName, field, value); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); const result = await collection.deleteOne({ [field]: value }); log.DEBUG("Document deleted:", result.deletedCount); return result.deletedCount; } catch (error) { console.error("Error deleting document:", error); throw error; } finally { await db.close(); } };