Linting
All checks were successful
release-tag / release-image (push) Successful in 1m52s
Lint JavaScript/Node.js / lint-js (push) Successful in 11s
DRB Tests / drb_mocha_tests (push) Successful in 29s

This commit is contained in:
Logan Cusano
2024-08-11 15:57:46 -04:00
parent 5cd47378d6
commit 117cbea67f
37 changed files with 2273 additions and 1738 deletions

View File

@@ -1,10 +1,10 @@
// Import necessary modules
import { MongoClient } from 'mongodb';
import { DebugBuilder } from '../debugger.mjs';
const log = new DebugBuilder("server", 'mongoHandler');
import { MongoClient } from "mongodb";
import { DebugBuilder } from "../debugger.mjs";
const log = new DebugBuilder("server", "mongoHandler");
import dotenv from 'dotenv';
dotenv.config()
import dotenv from "dotenv";
dotenv.config();
// MongoDB connection URI
const uri = process.env.MONGO_URL;
@@ -15,7 +15,7 @@ export const connectToDatabase = async () => {
const client = await MongoClient.connect(uri);
return client;
} catch (error) {
console.error('Error connecting to the database:', error);
console.error("Error connecting to the database:", error);
throw error;
}
};
@@ -24,13 +24,13 @@ export const connectToDatabase = async () => {
export const insertDocument = async (collectionName, document) => {
const db = await connectToDatabase();
log.DEBUG("Inserting document:", collectionName, document);
try {
try {
const collection = db.db().collection(collectionName);
const result = await collection.insertOne(document);
log.DEBUG('Document inserted:', result.insertedId);
log.DEBUG("Document inserted:", result.insertedId);
return result.insertedId;
} catch (error) {
console.error('Error inserting document:', error);
console.error("Error inserting document:", error);
throw error;
} finally {
// Close the connection
@@ -42,13 +42,13 @@ export const insertDocument = async (collectionName, document) => {
export const getDocuments = async (collectionName) => {
log.DEBUG("Getting all documents:", collectionName);
const db = await connectToDatabase();
try {
try {
const collection = db.db().collection(collectionName);
const documents = await collection.find({}).toArray();
log.DEBUG('Documents retrieved:', documents);
log.DEBUG("Documents retrieved:", documents);
return documents;
} catch (error) {
console.error('Error retrieving documents:', error);
console.error("Error retrieving documents:", error);
throw error;
} finally {
// Close the connection
@@ -65,7 +65,7 @@ export const getDocumentByField = async (collectionName, field, value) => {
const document = await collection.findOne({ [field]: value });
return document;
} catch (error) {
console.error('Error retrieving document:', error);
console.error("Error retrieving document:", error);
throw error;
} finally {
await db.close();
@@ -73,16 +73,30 @@ 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);
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);
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);
console.error("Error updating document:", error);
throw error;
} finally {
await db.close();
@@ -96,12 +110,12 @@ export const deleteDocumentByField = async (collectionName, field, value) => {
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({ [field]: value });
log.DEBUG('Document deleted:', result.deletedCount);
log.DEBUG("Document deleted:", result.deletedCount);
return result.deletedCount;
} catch (error) {
console.error('Error deleting document:', error);
console.error("Error deleting document:", error);
throw error;
} finally {
await db.close();
}
};
};