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(); } };