mirrorClasses()
mirrorClasses, mirrorDoc
Function Name
mirrorClasses
Trigger
This Cloud Function is triggered by Firestore document write events (including creation, updates, or deletions) at the path classes/{documentId}
. It responds to any change made to documents within the classes
collection.
Configuration
- Document Path:
classes/{documentId}
- Specifies the Firestore path that triggers the function. Changes to any document within this path will initiate the function.
- Timeout: 180 seconds
- The function is configured with an extended timeout to handle potentially complex or large data manipulations.
Operation
The mirrorClasses
function is instantiated by generateMirrorDocFunction
with "classes"
as the target collection. It operates as follows:
- Event Capture: Captures write events to documents in the
classes
collection. - Data Handling:
- Deletion: If a document is deleted (
snapshot.after.data()
returns null), the corresponding document in the mirrored collection is also deleted to ensure both collections remain in sync. - Creation/Update: If a document is added or modified, the function retrieves the new document data and replicates it in the specified mirrored collection using the same document ID.
- Deletion: If a document is deleted (
Example Execution Flow
- Document Created/Updated: When a document in the
classes
collection is either created or updated, its content is mirrored to another collection designated for similar or related operational needs. - Document Deleted: Corresponds to the removal of a class document, leading to an automatic deletion of its counterpart in the mirrored collection.
Helper Function: generateMirrorDocFunction
- Purpose: To dynamically generate a Firestore trigger function configured for a specific collection. This function is designed to handle document write events and ensure that changes are mirrored to a designated collection.
- Usage in
mirrorClasses
:- The function generated for the
classes
collection handles the synchronization tasks specifically for documents within this collection, as defined by the initial configuration passed togenerateMirrorDocFunction
.
- The function generated for the
Error Handling and Security
- Error Handling: Includes mechanisms to handle null data cases (deletions) and errors during database operations, such as failed writes due to permission issues or network problems.
- Security Considerations:
- Permissions: The function must have sufficient permissions to read from and write to both the source and the target Firestore collections.
- Data Integrity: Ensure robust validation and error-checking to maintain data consistency and integrity between the mirrored collections.
Deployment and Maintenance
This function is deployed automatically using GitHub actions, whenever changes are merged into the main
branch.