Sync actions
Sync actions focused on synchronization processes, such as starting sync operations, managing sync scopes, or deleting sync-related data. These actions ensure data consistency across different parts of the system.
Types of Syncs
action.delete-sync-scope
Deletes a specific sync scope from the database table using its sync-scope ID.
action.delete-sync-status
Deletes a specific sync status from the database table using its sync-status ID.
action.start-sync-scope
Starts or restarts a specific sync scope. The scope completes once all defined sync-entity actions/datasources have finished.
Key Components
1. The _syncStatus Table
This table tracks the status of individual sync operations with the following properties:
id
Unique identifier for the sync operation.
provider
The data provider being used.
entity
The entity being synchronized.
functionId
The function handling the sync.
state
Current sync state, which is either:
syncingsyncedfailed
timestamp
When the last status change occurred.
error
Contains any errors that occurred during sync.
2. The _syncScope Table
This table tracks declared sync scopes and monitors their overall progress. The table allows a sync scope to be started and use a datasource to track the progress of the sync scope.
id
Comes from the instanceId of the action.start-sync-scope
state
Overall scope state, which is either:
syncing: Not all instances completed (i.e. failed or succeeded), some may not have started yet.failed: All instances completed, at least one failedsynced: All instances completed successfullyinterrupted: App was closed, crashed, or put in the background
notStarted
Count of instances not yet started.
syncing
Count of instances currently syncing, and have not completed or been interrupted.
failed
Count of instances that completed with errors.
synced
Count of instances that completed successfully.
lastStartedAt
Timestamp when scope last started (epoch milliseconds).
lastFailedAt
Timestamp when scope last failed (epoch milliseconds).
lastSyncedAt
Timestamp when scope last succeeded (epoch milliseconds).
details
JSON structure with per-instance details and failure information.
How to use sync scopes
Step 1: Define the action-start-sync-scope
Use action.start-sync-scope to initialize a sync scope with a unique instanceId:
Step 2: Define individual sync actions
Each sync operation needs its own action.sync-entities with a matching instanceId:
Step 3: Conditionally trigger sync
Prevent multiple simultaneous syncs using a condition:
Monitoring Sync Progress
Query _syncStatus for individual operations
Query _syncScope for overall progress
Benefits
Coordinated syncing: Manage multiple entity syncs as a single operation.
Progress tracking: Monitor counts of syncing, synced, failed, and pending operations.
Error Handling: Track failures with detailed error information.
State Management: Prevent duplicate syncs and handle interruptions.
Historical Data: Access timestamps for sync events and detailed operation history.
Best practices
Always assign unique
instanceIdvalues to each sync operation.Use conditional logic to prevent overlapping sync operations.
Monitor the
_syncScopestate to provide user feedback.Handle the
interruptedstate for better user experience when apps are put in the background.Check the
detailscolumn for granular failure information.
Last updated
Was this helpful?