Usage Examples
Code snippets and guidelines for common scenarios
Multiple Tab Support
Multiple tab support is not currently available on Android or Safari.
Using PowerSync between multiple tabs is supported on some web browsers. Multiple tab support relies on shared web workers for database and sync streaming operations. When enabled, shared web workers named shared-DB-worker-[dbFileName]
and shared-sync-[dbFileName]
will be created.
shared-DB-worker-[dbFileName]
The shared database worker will ensure writes to the database will instantly be available between tabs.
shared-sync-[dbFileName]
The shared sync worker connects directly to the PowerSync backend instance and applies changes to the database. Note that the shared sync worker will call the fetchCredentials
and uploadData
method of the latest opened available tab. Closing a tab will shift the latest tab to the previously opened one.
Currently, using the SDK in multiple tabs without enabling the enableMultiTabs flag will spawn a standard web worker per tab for DB operations. These workers are safe to operate on the DB concurrently, however changes from one tab may not update watches on other tabs. Only one tab can sync from the PowerSync instance at a time. The sync status will not be shared between tabs, only the oldest tab will connect and display the latest sync status.
Support is enabled by default if available. This can be disabled as below:
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).
PowerSyncDatabase.writeTransaction(callback) automatically commits changes after the transaction callback is completed if tx.rollback()
has not explicitly been called. If an exception is thrown in the callback then changes are automatically rolled back.
Also see PowerSyncDatabase.readTransaction(callback).
Subscribe to changes in data
Use PowerSyncDatabase.watch to watch for changes in source tables.
The watch
method can be used with a AsyncIterable
signature as follows:
As of version 1.3.3 of the SDK, the watch
method can also be used with a callback:
Insert, update, and delete data in the local database
Use PowerSyncDatabase.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 PowerSyncDatabase.connected and register an event listener with PowerSyncDatabase.registerListener to listen for status changes to your PowerSync instance.
Wait for the initial sync to complete
Use the hasSynced property (available since version 0.4.1 of the SDK) and register an event listener with PowerSyncDatabase.registerListener to indicate to the user whether the initial sync is in progress.
For async use cases, see PowerSyncDatabase.waitForFirstSync(), which returns a promise that resolves once the first full sync has completed (it queries the internal SQL ps_buckets table to determine if data has been synced).
Using logging to troubleshoot issues
You can enable logging to see what’s happening under the hood or to debug connection/authentication/sync issues. This SDK uses js-logger.
Enable JS Logger with your logging interface of choice or use the default console
.
Enable verbose output in the developer tools for detailed logs.
The WASQLiteDBAdapter opens SQLite connections inside a shared web worker. This worker can be inspected in Chrome by accessing:
Was this page helpful?