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