diff --git a/modules/mongo-wrappers/mongoHandler.mjs b/modules/mongo-wrappers/mongoHandler.mjs index 359d16d..a22ba04 100644 --- a/modules/mongo-wrappers/mongoHandler.mjs +++ b/modules/mongo-wrappers/mongoHandler.mjs @@ -1,5 +1,7 @@ // Import necessary modules import { MongoClient } from 'mongodb'; +import { DebugBuilder } from '../debugger.mjs'; +const log = new DebugBuilder("server", 'mongoHandler'); import dotenv from 'dotenv'; dotenv.config() @@ -21,10 +23,11 @@ export const connectToDatabase = async () => { // 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); - console.log('Document inserted:', result.insertedId); + log.DEBUG('Document inserted:', result.insertedId); return result.insertedId; } catch (error) { console.error('Error inserting document:', error); @@ -37,11 +40,12 @@ export const insertDocument = async (collectionName, document) => { // 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(); - console.log('Documents retrieved:', documents); + log.DEBUG('Documents retrieved:', documents); return documents; } catch (error) { console.error('Error retrieving documents:', error); @@ -54,6 +58,7 @@ export const getDocuments = async (collectionName) => { // 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); @@ -69,11 +74,12 @@ export const getDocumentByField = async (collectionName, field, value) => { // 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 }); - console.log('Document updated:', result.modifiedCount); + log.DEBUG('Document updated:', result.modifiedCount); return result.modifiedCount; } catch (error) { console.error('Error updating document:', error); @@ -85,11 +91,12 @@ export const updateDocumentByField = async (collectionName, field, value, update // 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 }); - console.log('Document deleted:', result.deletedCount); + log.DEBUG('Document deleted:', result.deletedCount); return result.deletedCount; } catch (error) { console.error('Error deleting document:', error);