This commit is contained in:
Logan Cusano
2024-08-11 20:14:36 -04:00
parent 94374b4d45
commit cf49ac414a
6 changed files with 187 additions and 97 deletions

View File

@@ -1,82 +1,113 @@
import { getDocumentByField, deleteDocumentByField, getDocumentByFields, upsertDocumentByField, deleteDocumentByFields, upsertDocumentByFields } from './mongoHandler.mjs'; // Import your MongoDB handlers
import { DebugBuilder } from '../debugger.mjs';
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 log = new DebugBuilder("server", "mongoConfigWrappers");
const collectionName = 'configurations';
const collectionName = "configurations";
// Function to get a configuration by key
export const getConfig = async (key) => {
try {
const config = await getDocumentByField(collectionName, 'key', key);
log.DEBUG(`Configuration for key "${key}" retrieved:`, config);
return config ? config[key] : null; // Return null if no configuration is found
} catch (error) {
log.ERROR('Error retrieving configuration:', error);
throw error;
}
try {
const config = await getDocumentByField(collectionName, "key", key);
log.DEBUG(`Configuration for key "${key}" retrieved:`, config);
return config ? config[key] : 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) => {
// Set the config object
value = {key : value};
try {
const result = await upsertDocumentByField(collectionName, 'key', 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;
}
// Set the config object
value = { key: value };
try {
const result = await upsertDocumentByField(
collectionName,
"key",
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, 'key', 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;
}
try {
const result = await deleteDocumentByField(collectionName, "key", 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], ['key', key]);
log.DEBUG(`Guild ${guildId} configuration for key "${key}" retrieved:`, config);
return config ? config[key] : null; // Return null if no configuration is found
} catch (error) {
log.ERROR('Error retrieving guild configuration:', error);
throw error;
}
try {
const config = await getDocumentByFields(
collectionName,
["guildId", guildId],
["key", key],
);
log.DEBUG(
`Guild ${guildId} configuration for key "${key}" retrieved:`,
config,
);
return config ? config[key] : 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) => {
// Set the config object
value = {key : value};
try {
const result = await upsertDocumentByFields(collectionName, value, ['guildId', guildId], ['key', 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;
}
// Set the config object
value = { key: value };
try {
const result = await upsertDocumentByFields(
collectionName,
value,
["guildId", guildId],
["key", 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], ['key', 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;
}
try {
const result = await deleteDocumentByFields(
collectionName,
["guildId", guildId],
["key", 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;
}
};