softDeleteUserTrial()
softDeleteUserTrial, createSoftDeleteCloudFunction
Function Name
softDeleteUserTrial
Function Overview
1. createSoftDeleteCloudFunction
This utility function dynamically generates Cloud Functions designed to handle the soft deletion of documents by relocating them to "deleted" collections when they are removed from their primary collections. It provides a framework for creating functions that manage data retention effectively across various collection structures.
Operation
- Trigger Configuration: Utilizes the
constructTrigger
function to establish a Firestore trigger based on specified collections. - Soft Deletion Mechanism: Upon the deletion of a document, the generated function moves the document to a new path within a "deleted" collection, preserving its data.
2. softDeleteUserTrial
A specific Cloud Function generated by createSoftDeleteCloudFunction
, tailored to handle deletions within the nested users
, 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
softDeleteUserTrial
, it constructs a path likeusers/{doc0}/runs/{doc1}/trials/{doc2}
. - Dynamically creates path variables (
doc0
,doc1
,doc2
) corresponding to each collection level involved.
- 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 does not exit unexpectedly.
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.