Files
drb-server/modules/mongo-wrappers/mongoNodesWrappers.mjs
Logan Cusano 2ab5a181bd
All checks were successful
DRB Tests / drb_mocha_tests (pull_request) Successful in 32s
#5 replace all console.logs with debugger
2024-05-25 23:52:18 -04:00

77 lines
2.2 KiB
JavaScript

import { DebugBuilder } from "../../modules/debugger.mjs";
const log = new DebugBuilder("server", "mongoNodesWrappers");
import { insertDocument, getDocuments, connectToDatabase } from "./mongoHandler.mjs";
const collectionName = 'nodes';
// Wrapper for inserting a node
export const createNode = async (node) => {
try {
const insertedId = await insertDocument(collectionName, node);
return insertedId;
} catch (error) {
log.ERROR('Error creating node:', error);
throw error;
}
};
// Wrapper for retrieving all nodes
export const getAllNodes = async () => {
try {
const nodes = await getDocuments(collectionName);
return nodes;
} catch (error) {
log.ERROR('Error getting all nodes:', error);
throw error;
}
};
// Wrapper for retrieving a node by NUID
export const getNodeByNuid = async (nuid) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const node = await collection.findOne({ nuid });
return node;
} catch (error) {
log.ERROR('Error getting node by NUID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper for updating a node by NUID
export const updateNodeByNuid = async (nuid, updatedFields) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.updateOne({ nuid }, { $set: updatedFields });
log.INFO('Node updated:', result.modifiedCount);
return result.modifiedCount;
} catch (error) {
log.ERROR('Error updating node by NUID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper for deleting a node by NUID
export const deleteNodeByNuid = async (nuid) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({ nuid });
log.INFO('Node deleted:', result.deletedCount);
return result.deletedCount;
} catch (error) {
log.ERROR('Error deleting node by NUID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};