Move mongo wrappers to their own directory
All checks were successful
DRB Tests / drb_mocha_tests (push) Successful in 29s

This commit is contained in:
Logan Cusano
2024-05-18 23:16:42 -04:00
parent cfe2cd346d
commit 47070b08ab
9 changed files with 106 additions and 59 deletions

View File

@@ -0,0 +1,90 @@
import { insertDocument, getDocuments, connectToDatabase } from "./mongoHandler.mjs";
const collectionName = 'discord-ids';
// Wrapper for inserting a Discord ID
export const createDiscordID = async (discordID) => {
try {
const insertedId = await insertDocument(collectionName, discordID);
return insertedId;
} catch (error) {
console.error('Error creating Discord ID:', error);
throw error;
}
};
// Wrapper for retrieving all Discord IDs
export const getAllDiscordIDs = async () => {
try {
const discordIDs = await getDocuments(collectionName);
return discordIDs;
} catch (error) {
console.error('Error getting all Discord IDs:', error);
throw error;
}
};
// Wrapper for retrieving a Discord ID by name or discord_id
export const getDiscordID = async (identifier) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const discordID = await collection.findOne({
$or: [
{ name: identifier },
{ discord_id: identifier }
]
});
return discordID;
} catch (error) {
console.error('Error getting Discord ID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper for updating a Discord ID by name or discord_id
export const updateDiscordID = async (identifier, updatedFields) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.updateOne({
$or: [
{ name: identifier },
{ discord_id: identifier }
]
}, { $set: updatedFields });
console.log('Discord ID updated:', result.modifiedCount);
return result.modifiedCount;
} catch (error) {
console.error('Error updating Discord ID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper for deleting a Discord ID by name or discord_id
export const deleteDiscordID = async (identifier) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({
$or: [
{ name: identifier },
{ discord_id: identifier }
]
});
console.log('Discord ID deleted:', result.deletedCount);
return result.deletedCount;
} catch (error) {
console.error('Error deleting Discord ID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};

View File

@@ -0,0 +1,100 @@
// 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();
}
};

View File

@@ -0,0 +1,75 @@
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) {
console.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) {
console.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) {
console.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 });
console.log('Node updated:', result.modifiedCount);
return result.modifiedCount;
} catch (error) {
console.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 });
console.log('Node deleted:', result.deletedCount);
return result.deletedCount;
} catch (error) {
console.error('Error deleting node by NUID:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};

View File

@@ -0,0 +1,111 @@
import { insertDocument, getDocuments, connectToDatabase } from "./mongoHandler.mjs";
const collectionName = 'radio-systems';
// Local wrapper to remove any local files from radio systems
const removeLocalFilesFromsystem = async (system) => {
if (system.trunkFile) delete system.trunkFile;
if (system.whitelistFile) delete system.whitelistFile;
}
// Wrapper for inserting a system
export const createSystem = async (name, system, nuid) => {
try {
// Remove any local files
await removeLocalFilesFromsystem(system);
// Add the NUID of the node that created this system
system.nodes = [nuid];
// Add the name of the system
system.name = name
const insertedId = await insertDocument(collectionName, system);
return insertedId;
} catch (error) {
console.error('Error creating system:', error);
throw error;
}
};
// Wrapper for retrieving all systems
export const getAllSystems = async () => {
try {
const systems = await getDocuments(collectionName);
return systems;
} catch (error) {
console.error('Error getting all systems:', error);
throw error;
}
};
// Wrapper for retrieving a system by name
export const getSystemByName = async (name) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const system = await collection.findOne({ name });
return system;
} catch (error) {
console.error('Error getting system by name:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper to get all systems from a given node
export const getSystemsByNuid = async (nuid) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
// Query for documents where the 'nodes' array contains the given nodeID
const query = { nodes: nuid };
const systems = await collection.find(query).toArray();
return systems;
} catch (error) {
console.error('Error finding entries:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper for updating a system by name
export const updateSystemByName = async (name, updatedSystem) => {
// Remove any local files
await removeLocalFilesFromsystem(updatedSystem);
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.updateOne({ name }, { $set: updatedSystem });
console.log('System updated:', result.modifiedCount);
return result.modifiedCount;
} catch (error) {
console.error('Error updating system by name:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Wrapper for deleting a system by name
export const deleteSystemByName = async (name) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({ name });
console.log('System deleted:', result.deletedCount);
return result.deletedCount;
} catch (error) {
console.error('Error deleting system by name:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};