Improve Mongo Handler

- Added delete/update/get with multiple fields
- Updated single field handlers to use multi field handlers to limit variation
- Added upsert function to wrap the update function with `upsert: true`
This commit is contained in:
Logan Cusano
2024-08-11 18:34:55 -04:00
parent e324ee1738
commit f4886f9fc5

View File

@@ -59,10 +59,23 @@ export const getDocuments = async (collectionName) => {
// Function to retrieve a document by a specific field
export const getDocumentByField = async (collectionName, field, value) => {
log.DEBUG("Getting document by field:", collectionName, field, value);
return await getDocumentByFields(collectionName, [field, value]);
};
// Function to retrieve a document by multiple fields
export const getDocumentByFields = async (collectionName, ...fieldValuePairs) => {
log.DEBUG("Getting document by fields:", collectionName, fieldValuePairs);
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const document = await collection.findOne({ [field]: value });
// Convert the fieldValuePairs array into an object
const query = fieldValuePairs.reduce((acc, [field, value]) => {
acc[field] = value;
return acc;
}, {});
const document = await collection.findOne(query);
return document;
} catch (error) {
console.error("Error retrieving document:", error);
@@ -72,12 +85,45 @@ export const getDocumentByField = async (collectionName, field, value) => {
}
};
// Function to update a document by a specific field
export const upsertDocumentByField = async (
collectionName,
field,
value,
updatedFields,
) => {
log.DEBUG(
"Upsert document by field:",
collectionName,
field,
value,
updatedFields,
);
return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, [field, value]);
};
// Function to update a document by a specific field
export const upsertDocumentByFields = async (
collectionName,
updatedFields,
...fieldValuePairs
) => {
log.DEBUG(
"Upsert document by fields:",
collectionName,
updatedFields,
fieldValuePairs
);
return await updateDocumentByFields(collectionName, updatedFields, { upsert: true }, fieldValuePairs);
};
// Function to update a document by a specific field
export const updateDocumentByField = async (
collectionName,
field,
value,
updatedFields,
options = null,
) => {
log.DEBUG(
"Update document by field:",
@@ -85,13 +131,39 @@ export const updateDocumentByField = async (
field,
value,
updatedFields,
options,
);
return await updateDocumentByFields(collectionName, updatedFields, options, [field, value])
};
// Function to update a document by multiple fields
export const updateDocumentByFields = async (
collectionName,
updatedFields,
options,
...fieldValuePairs
) => {
log.DEBUG(
"Update document by fields:",
collectionName,
updatedFields,
options,
fieldValuePairs,
);
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
// Convert the fieldValuePairs array into an object
const query = fieldValuePairs.reduce((acc, [field, value]) => {
acc[field] = value;
return acc;
}, {});
const result = await collection.updateOne(
{ [field]: value },
query,
{ $set: updatedFields },
options
);
log.DEBUG("Document updated:", result.modifiedCount);
return result.modifiedCount;
@@ -106,10 +178,23 @@ export const updateDocumentByField = async (
// Function to delete a document by a specific field
export const deleteDocumentByField = async (collectionName, field, value) => {
log.DEBUG("Delete document by field:", collectionName, field, value);
return await deleteDocumentByFields(collectionName, [field, value]);
};
// Function to delete a document by multiple fields
export const deleteDocumentByFields = async (collectionName, ...fieldValuePairs) => {
log.DEBUG("Delete document by fields:", collectionName, fieldValuePairs);
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({ [field]: value });
// Convert the fieldValuePairs array into an object
const query = fieldValuePairs.reduce((acc, [field, value]) => {
acc[field] = value;
return acc;
}, {});
const result = await collection.deleteOne(query);
log.DEBUG("Document deleted:", result.deletedCount);
return result.deletedCount;
} catch (error) {
@@ -118,4 +203,4 @@ export const deleteDocumentByField = async (collectionName, field, value) => {
} finally {
await db.close();
}
};
};