Introduction
We recommend starting with our Sync Rules from First Principles blog post, which explains on a high-level what Sync Rules are, why they exist and how to implement them. The remainder of these docs dive further into the details.Sync Streams available in Early AlphaSync Streams are now available in early alpha! Sync Streams will eventually replace Sync Rules and are designed to allow for more dynamic syncing, while not compromising on existing offline-first capabilties.Key improvements in Sync Streams over Sync Rules include:
- On-demand syncing: You define Sync Streams on the PowerSync Service, and a client can then subscribe to them one or more times with different parameters.
- Temporary caching-like behavior: Each subscription includes a configurable ttl that keeps data active after your app unsubscribes, acting as a warm cache for recently accessed data.
- Simpler developer experience: Simplified syntax and mental model, and capabilities such as your UI components automatically managing subscriptions (for example, React hooks).
Defining Sync Rules
Each PowerSync Service instance has a Sync Rules configuration where Sync Rules are defined using SQL-like queries (limitations and more info here) combined together in a YAML file. This SQL-like syntax is used when connecting to either Postgres, MongoDB or MySQL as the backend source database. The PowerSync Service uses these SQL-like queries to group data into “sync buckets” when replicating data to client devices.
Sync rules are configured per PowerSync instance to enable partial sync.
- Selecting tables/collections and columns/fields to sync.
- Filtering data according to user ID.
- Filter data with static conditions.
- Filter data with custom parameters (from the JWT or from clients directly)
- Transforming column/field values.
Replication Into Sync Buckets
PowerSync replicates and transforms relevant data from the backend source database according to Sync Rules. Data from this step is persisted in separate sync buckets on the PowerSync Service. Data is incrementally updated so that sync buckets always contain current state data as well as a full history of changes.
Data is replicated from the source database into sync buckets.
Client Database Hydration
PowerSync asynchronously hydrates local SQLite databases embedded in the PowerSync Client SDK based on data in sync buckets.
Data flow showing sync buckets which are created based on sync rules.