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

View File

@@ -63,7 +63,10 @@ export const getDocumentByField = async (collectionName, field, value) => {
};
// Function to retrieve a document by multiple fields
export const getDocumentByFields = async (collectionName, ...fieldValuePairs) => {
export const getDocumentByFields = async (
collectionName,
...fieldValuePairs
) => {
log.DEBUG("Getting document by fields:", collectionName, fieldValuePairs);
const db = await connectToDatabase();
try {
@@ -99,7 +102,12 @@ export const upsertDocumentByField = async (
value,
updatedFields,
);
return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, [field, value]);
return await updateDocumentByFields(
collectionName,
updatedFields,
{ upsert: true },
[field, value],
);
};
// Function to update a document by a specific field
@@ -112,9 +120,14 @@ export const upsertDocumentByFields = async (
"Upsert document by fields:",
collectionName,
updatedFields,
fieldValuePairs
fieldValuePairs,
);
return await updateDocumentByFields(
collectionName,
updatedFields,
{ upsert: true },
fieldValuePairs,
);
return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, fieldValuePairs);
};
// Function to update a document by a specific field
@@ -133,7 +146,10 @@ export const updateDocumentByField = async (
updatedFields,
options,
);
return await updateDocumentByFields(collectionName, updatedFields, options, [field, value])
return await updateDocumentByFields(collectionName, updatedFields, options, [
field,
value,
]);
};
// Function to update a document by multiple fields
@@ -163,7 +179,7 @@ export const updateDocumentByFields = async (
const result = await collection.updateOne(
query,
{ $set: updatedFields },
options
options,
);
log.DEBUG("Document updated:", result.modifiedCount);
return result.modifiedCount;
@@ -182,7 +198,10 @@ export const deleteDocumentByField = async (collectionName, field, value) => {
};
// Function to delete a document by multiple fields
export const deleteDocumentByFields = async (collectionName, ...fieldValuePairs) => {
export const deleteDocumentByFields = async (
collectionName,
...fieldValuePairs
) => {
log.DEBUG("Delete document by fields:", collectionName, fieldValuePairs);
const db = await connectToDatabase();
try {
@@ -203,4 +222,4 @@ export const deleteDocumentByFields = async (collectionName, ...fieldValuePairs)
} finally {
await db.close();
}
};
};