Usage Examples
Code snippets and guidelines for common scenarios
Using transactions to group changes
Read and write transactions present a context where multiple changes can be made then finally committed to the DB or rolled back. This ensures that either all the changes get persisted, or no change is made to the DB (in the case of a rollback or exception).
The writeTransaction(callback) method combines all writes into a single transaction, only committing to persistent storage once.
Also see readTransaction(callback) .
Subscribe to changes in data
Use watch to watch for changes to the dependent tables of any SQL query.
Insert, update, and delete data in the local database
Use execute to run INSERT, UPDATE or DELETE queries.
Send changes in local data to your backend service
Override uploadData to send local updates to your backend service.
Accessing PowerSync connection status information
Use SyncStatus and register an event listener with statusStream to listen for status changes to your PowerSync instance.
Wait for the initial sync to complete
Use the hasSynced property (available since version 1.5.1 of the SDK) and register a listener to indicate to the user whether the initial sync is in progress.
For async use cases, see the waitForFirstSync method which returns a promise that resolves once the first full sync has completed.
Using logging to troubleshoot issues
Since version 1.1.2 of the SDK, logging is enabled by default and outputs logs from PowerSync to the console in debug mode.
Was this page helpful?