From a56c19a4668e1a3fa9e835c9db0eb0e71dece770 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 11 Aug 2024 18:38:21 -0400 Subject: [PATCH] Added mongo wrapper for configs #19 - Can handle discord guid specific configs and global configs --- .../mongo-wrappers/mongoConfigWrappers.mjs | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 modules/mongo-wrappers/mongoConfigWrappers.mjs diff --git a/modules/mongo-wrappers/mongoConfigWrappers.mjs b/modules/mongo-wrappers/mongoConfigWrappers.mjs new file mode 100644 index 0000000..65c0ea1 --- /dev/null +++ b/modules/mongo-wrappers/mongoConfigWrappers.mjs @@ -0,0 +1,78 @@ +import { getDocumentByField, deleteDocumentByField, getDocumentByFields, upsertDocumentByField, deleteDocumentByFields, upsertDocumentByFields } from './mongoHandler.mjs'; // Import your MongoDB handlers +import { DebugBuilder } from '../debugger.mjs'; + +const log = new DebugBuilder("server", 'mongoConfigWrappers'); + +const collectionName = 'configurations'; + +// Function to get a configuration by key +export const getConfig = async (key) => { + try { + const config = await getDocumentByField(collectionName, '_id', key); + log.DEBUG(`Configuration for key "${key}" retrieved:`, config); + return config || null; // Return null if no configuration is found + } catch (error) { + log.ERROR('Error retrieving configuration:', error); + throw error; + } +}; + +// Function to set a configuration by key +export const setConfig = async (key, value) => { + try { + const result = await upsertDocumentByField(collectionName, '_id', key, value); + log.DEBUG(`Configuration for key "${key}" set:`, value, result); + return result > 0 ? key : null; // Return key if updated successfully, otherwise null + } catch (error) { + log.ERROR('Error setting configuration:', error); + throw error; + } +}; + +// Function to delete a configuration by key (optional) +export const deleteConfig = async (key) => { + try { + const result = await deleteDocumentByField(collectionName, '_id', key); + log.DEBUG(`Configuration for key "${key}" deleted:`, result); + return result; // Return the count of deleted documents + } catch (error) { + log.ERROR('Error deleting configuration:', error); + throw error; + } +}; + +// Function to get a configuration by key for a specific guild +export const getGuildConfig = async (guildId, key) => { + try { + const config = await getDocumentByFields(collectionName, ['guildId', guildId], ['_id', key]); + log.DEBUG(`Guild ${guildId} configuration for key "${key}" retrieved:`, config); + return config || null; // Return null if no configuration is found + } catch (error) { + log.ERROR('Error retrieving guild configuration:', error); + throw error; + } +}; + +// Function to set a configuration by key for a specific guild +export const setGuildConfig = async (guildId, key, value) => { + try { + const result = await upsertDocumentByFields(collectionName, value, ['guildId', guildId], ['_id', key]); + log.DEBUG(`Guild ${guildId} configuration for key "${key}" set:`, value); + return result > 0 ? key : null; // Return key if updated successfully, otherwise null + } catch (error) { + log.ERROR('Error setting guild configuration:', error); + throw error; + } +}; + +// Function to delete a configuration by key for a specific guild (optional) +export const deleteGuildConfig = async (guildId, key) => { + try { + const result = await deleteDocumentByFields(collectionName, ['guildId', guildId], ['_id', key]); + log.DEBUG(`Guild ${guildId} configuration for key "${key}" deleted:`, result); + return result; // Return the count of deleted documents + } catch (error) { + log.ERROR('Error deleting guild configuration:', error); + throw error; + } +};