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 capabilities. See the Overview page for more details.
Key Concepts
Bucket Definition
Sync Rules are a set of bucket definitions. A bucket definition defines the actual individual buckets that will be created by the PowerSync Service when it replicates data from your source database. Clients then sync the individual buckets that are relevant to them based on their parameters (see below). Each bucket definition consists of:- A custom name for the bucket, e.g
user_lists - Zero or more Parameter Queries which are used to explicitly select the parameters for the bucket. If no Parameter Query is specified in the bucket definition, it’s automatically a global bucket.
- One or more Data Queries which selects the data for the bucket. Data Queries can make use of the parameters that are selected in the Parameter Query.
Sync Rules with a single bucket definition
Parameters
A Parameter is a value that can be used in the Sync Rules to create dynamic sync behavior for each user/client. Each client syncs only the relevant buckets based on the parameters for that client (i.e. the results that would be returned from the Parameter Query for that client).Parameter Queries
Parameter Queries are SQL-like queries that explicitly define the parameters for a bucket. The following values can be selected in Parameter Queries:- Authentication Parameters (see below)
- Client Parameters (see below)
- Values From a Table/Collection (see below)
See Parameter Queries for more details and examples. Also see Supported SQL for limitations.
Authentication Parameters
This is a set of parameters specified in the user’s JWT authentication token, and they are signed as part of the JWT (see Authentication). This always includes the JWT subject (sub) which is the user ID, but may include additional and custom parameters (other claims in the JWT). Authentication Parameters are used to identify the user, and specify permissions for the user. They need to be explicitly selected in the Parameter Query to be used in Sync Rules:
Example of selecting Authentication Parameter in a Parameter Query
See Parameter Queries for more details and examples.
Client Parameters
Clients can specify Client Parameters when connecting to PowerSync (i.e. whenconnect() is called). Client Parameters need to be explicitly selected in the Parameter Query to be used in Sync Rules:
Example of selecting a Client Parameter in a Parameter Query
See Client Parameters and Parameter Queries for more details and examples.
Values From a Table/Collection
A Parameter Query can select a parameter from a table/collection in your source database, e.g.:Selecting a parameter from a table in the source database
See Parameter Queries for more details and examples.
Data Queries
Data Queries select the data for the bucket, and can make use of any of the bucket parameters (i.e. values returned by the Parameter Queries). When referencing a parameter, the syntaxbucket.<parameter-name> should be used in the Data Query:
See Data Queries for more details and examples. Also see Supported SQL for limitations.
Global Buckets
If no Parameter Query is specified in the bucket definition, the bucket is automatically a global bucket. These buckets will be synced to all clients/users.See Global Buckets for more details and examples.
Potential Parameter Values Determine Created Buckets
When your PowerSync Service instance replicates data from your source database based on your Sync Rules configuration (i.e. your bucket definitions), it finds all possible values for your defined parameters in the relevant tables/collections in your source database, and creates individual buckets based on those values. For example, let’s say you have a bucket nameduser_todo_lists that contains the to-do lists for a user, and that bucket utilizes a user_id parameter (which will be obtained from the JWT) to scope those to-do lists. Now let’s say users with IDs 1, 2 and 3 exist in the source database. PowerSync will then replicate data from the source database and preemptively create individual buckets with bucket IDs of user_todo_lists["1"], user_todo_lists["2"] and user_todo_lists["3"].
This architecture is key to the scalability and performance of PowerSync. See the PowerSync Service architecture overview for more background.