// 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); return await getDocumentByFields(collectionName, [field, value]); }; // Function to retrieve a document by multiple fields export const getDocumentByFields = async (collectionName, ...fieldValuePairs) => { log.DEBUG("Getting document by fields:", collectionName, fieldValuePairs); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); // Convert the fieldValuePairs array into an object const query = fieldValuePairs.reduce((acc, [field, value]) => { acc[field] = value; return acc; }, {}); const document = await collection.findOne(query); 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 upsertDocumentByField = async ( collectionName, field, value, updatedFields, ) => { log.DEBUG( "Upsert document by field:", collectionName, field, value, updatedFields, ); return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, [field, value]); }; // Function to update a document by a specific field export const upsertDocumentByFields = async ( collectionName, updatedFields, ...fieldValuePairs ) => { log.DEBUG( "Upsert document by fields:", collectionName, updatedFields, fieldValuePairs ); return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, fieldValuePairs); }; // Function to update a document by a specific field export const updateDocumentByField = async ( collectionName, field, value, updatedFields, options = null, ) => { log.DEBUG( "Update document by field:", collectionName, field, value, updatedFields, options, ); return await updateDocumentByFields(collectionName, updatedFields, options, [field, value]) }; // Function to update a document by multiple fields export const updateDocumentByFields = async ( collectionName, updatedFields, options, ...fieldValuePairs ) => { log.DEBUG( "Update document by fields:", collectionName, updatedFields, options, fieldValuePairs, ); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); // Convert the fieldValuePairs array into an object const query = fieldValuePairs.reduce((acc, [field, value]) => { acc[field] = value; return acc; }, {}); const result = await collection.updateOne( query, { $set: updatedFields }, options ); 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); return await deleteDocumentByFields(collectionName, [field, value]); }; // Function to delete a document by multiple fields export const deleteDocumentByFields = async (collectionName, ...fieldValuePairs) => { log.DEBUG("Delete document by fields:", collectionName, fieldValuePairs); const db = await connectToDatabase(); try { const collection = db.db().collection(collectionName); // Convert the fieldValuePairs array into an object const query = fieldValuePairs.reduce((acc, [field, value]) => { acc[field] = value; return acc; }, {}); const result = await collection.deleteOne(query); log.DEBUG("Document deleted:", result.deletedCount); return result.deletedCount; } catch (error) { console.error("Error deleting document:", error); throw error; } finally { await db.close(); } };