Files
drb-server/modules/mongo-wrappers/mongoHandler.mjs
Logan Cusano 47070b08ab
All checks were successful
DRB Tests / drb_mocha_tests (push) Successful in 29s
Move mongo wrappers to their own directory
2024-05-18 23:16:42 -04:00

100 lines
3.0 KiB
JavaScript

// Import necessary modules
import { MongoClient } from 'mongodb';
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();
try {
const collection = db.db().collection(collectionName);
const result = await collection.insertOne(document);
console.log('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) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const documents = await collection.find({}).toArray();
console.log('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) => {
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) => {
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);
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) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({ [field]: value });
console.log('Document deleted:', result.deletedCount);
return result.deletedCount;
} catch (error) {
console.error('Error deleting document:', error);
throw error;
} finally {
await db.close();
}
};