softDeleteUserRun()
softDeleteUserRun, createSoftDeleteCloudFunction
Function Name
sotDeleteUserRun
1. createSoftDeleteCloudFunction
This utility function dynamically generates Cloud Functions aimed at handling the soft deletion of documents by relocating them to designated "deleted" collections when they are removed from their primary collections. It serves to maintain data integrity by preserving deleted documents in an organized manner.
Operation
- Trigger Configuration: Utilizes the
constructTrigger
function to establish a Firestore trigger based on specified collections. - Document Handling: Upon the deletion of a document, the generated function transfers the document to a new path within a "deleted" collection, thus preserving its data.
2. softDeleteUserRun
This specific Cloud Function generated by createSoftDeleteCloudFunction
is tailored to handle deletions within the nested structure of users
and runs
collections. It ensures that run data associated with users is preserved in an orderly fashion after deletion.
Detailed Workflow and Implementation
Trigger Construction (
constructTrigger
):- Develops a Firestore document path that listens for deletion events within the specified collections. For
softDeleteUserRun
, it configures a path likeusers/{doc0}/runs/{doc1}
. - Dynamically assigns path variables (
doc0
,doc1
) to correspond to each collection level involved.
- Develops a Firestore document path that listens for deletion events within the specified collections. For
Document References Creation (
createDocRefs
):- Generates Firestore document references for both the source (original location) and target (new location within the "deleted" collection) based on the structured document path.
- Ensures that documents are transferred from their active collection locations to appropriately named "deleted" collections.
Soft Deletion Handling:
- When a document deletion is detected, retrieves the document's data from the source.
- Moves the document data to the target location within the "deleted" collection, ensuring the preservation and archival of data.
Error Handling and Security Considerations
Error Handling:
- Robust error management during the document transfer to handle and log any failures, ensuring data is not lost during the operation.
- Accounts for scenarios where document data may be null or missing, providing graceful exits under such conditions.
Security:
- Implements security rules that ensure only authorized deletions prompt the soft deletion process, safeguarding against unauthorized data manipulation.
- Validates document paths and operations to ensure they adhere to security policies and prevent potential vulnerabilities.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.