softDeleteGuestTrial()
softDeleteGuestTrial, createSoftDeleteCloudFunction
Function Name
softDeleteGuestTrial
1. createSoftDeleteCloudFunction
This utility function dynamically generates Cloud Functions designed to handle the soft deletion of documents by moving them to corresponding "deleted" collections when they are deleted from their primary collection. It provides a framework for creating functions that manage data retention effectively across various collection structures.
Operation
- Trigger Configuration: Uses the
constructTrigger
function to create a Firestore trigger based on specified collections. - Soft Deletion Mechanism: On document deletion, the generated function moves the document to a new path within a "deleted" collection, preserving its data.
2. softDeleteGuestTrial
A specific Cloud Function generated by createSoftDeleteCloudFunction
, tailored to handle deletions within the nested guests
, runs
, and trials
collections. This function ensures that even deeply nested documents are preserved after deletion.
Detailed Workflow and Implementation
Trigger Construction (
constructTrigger
):- Configures a Firestore document path that listens for deletion events across specified collections. For
softDeleteGuestTrial
, it constructs a path likeguests/{doc0}/runs/{doc1}/trials/{doc2}
. - Dynamically creates path variables (
doc0
,doc1
,doc2
) corresponding to each collection level.
- Configures a Firestore document path that listens for deletion events across specified collections. For
Document References Creation (
createDocRefs
):- Generates Firestore document references for both the source and target locations based on the structured document path created by the trigger.
- Maps documents from their original location in active collections to new locations in corresponding "deleted" collections.
Soft Deletion Handling:
- Upon detecting a document's deletion, retrieves the document's data from the source reference.
- Moves the document data to the target path within the "deleted" collection, ensuring data retention and archival.
Error Handling and Security Considerations
Error Handling:
- Implements error handling during the document transfer process to manage and log failures, ensuring robust operation.
- Handles cases where document data might be null or missing, preventing failures and ensuring the function exits gracefully under such conditions.
Security:
- Enforces security rules to ensure that only authorized deletions trigger the soft deletion process, protecting against unauthorized data manipulation.
- Validates document paths and ensures that document operations conform to predefined security policies.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.