Skip to main content
These examples assume that your app already has a PowerSync database instance, as described in the Getting Started steps for your Client SDK. Examples may leave out other app setup for brevity. React examples that use PowerSync hooks require a PowerSyncContext.Provider above the component, with your database instance as its value.

Using Transactions to Group Changes

A transaction groups several writes into one unit. Either all of the writes are saved, or none of them are. PowerSync also queues the changes from one transaction for upload as a group. These examples delete a list and its todos together, so the app never keeps todos for a deleted list.
writeTransaction() runs your callback inside a transaction. PowerSync saves the changes when the callback finishes, and rolls them back if the callback throws an error.
Learn more:

Listen for Changes in Data

A watch query runs a SQL query again whenever the tables it reads from change, and gives you the new results. Use it to keep your UI up to date as data changes.
watch() gives you new results each time the query’s tables change. You can read the results with an async iterator (for await), or receive them in an onResult callback.
Learn more:

Insert, Update, and Delete Data in the Local Database

Write to the local SQLite database with standard SQL INSERT, UPDATE, and DELETE statements. PowerSync applies each write locally right away and queues it for upload to your backend. The insert examples use uuid(), a SQL function that PowerSync provides, to generate an ID for each new row.
execute() runs a SQL statement. The ? placeholders take their values from the parameter array. This React button inserts a new customer when clicked.
Learn more:

Send Changes in Local Data to Your Backend Service

PowerSync does not write to your source database directly. While the app is connected, the Client SDK calls the uploadData() method of your backend connector whenever local changes are waiting in the upload queue. Your implementation sends these changes to your backend API, which applies them to your source database. The examples below take the oldest queued transaction, send all of its changes to your backend, and then mark the transaction as complete. Each change includes the operation (PUT, PATCH, or DELETE), the table, the row ID, and the changed data. backendApi stands for your own API client. If the upload fails, throw an error. The SDK then keeps the changes in the queue and retries later. To generate a complete backend connector for your SDK, open your instance in the PowerSync Dashboard, select Client SDK Setup, and select your app language.
getNextCrudTransaction() returns the oldest transaction in the upload queue, or null if the queue is empty. Call complete() only after your backend has accepted the changes.
Learn more:

Accessing PowerSync Connection Status Information

The sync status tells you whether the app is connected to the PowerSync Service, and whether data is uploading or downloading. Use it to show a connection indicator in your UI.
useStatus() returns the current sync status and re-renders the component when it changes. Read connected to show the connection state.
Learn more:

Wait for the Initial Sync to Complete

When a user first opens your app, the local database has no synced data until the first sync finishes. PowerSync gives you two ways to know when that first full sync has completed:
  • hasSynced is a property of the sync status. It becomes true once the first full sync has completed. Use it in UI code that updates when the status changes, for example to show a loading screen instead of an empty list.
  • waitForFirstSync() waits until the first full sync has completed, then returns. Use it in code that needs the synced data before it continues, for example before a query that runs at startup.
If the first sync completed in an earlier app session, hasSynced is already true and waitForFirstSync() returns right away.
hasSynced is true once the first full sync has completed. useStatus() updates the component when that happens. To wait in code instead, call await db.waitForFirstSync().
Learn more:

Wait for Local Data to Catch Up

The first sync may have completed in an earlier app session, so hasSynced and waitForFirstSync() don’t tell you whether local data is up to date now. To wait until the local database has caught up with the server as of now, use Sync Catch-Up: request a checkpoint, then wait for it to sync. Most of these examples use it for a refresh action, so the refresh indicator stops only once the device has caught up. Checkpoint requests are in alpha. They require PowerSync Service v1.24.0 or later, and you must enable them with the checkpointMode option when calling connect(). See Prerequisites.
requestCheckpoint() asks the PowerSync Service for a checkpoint. waitForSync() resolves once the local database has synced that checkpoint. This React button stays disabled while it waits.
Learn more:

Report Sync Download Progress

While a download is in progress, the sync status reports how many operations have downloaded out of the total. Use it to show a progress bar, especially during a long first sync. When no download is active, there is no progress value. Several of these examples accept an optional priority. With a priority, they show progress toward the data with that priority and all higher priorities instead of the full sync. See Prioritized Sync.
useStatus() returns the current sync status and re-renders the component when it changes. downloadProgress.downloadedFraction is a value from 0.0 to 1.0. This example uses MUI components.
Learn more: