|
| 1 | +--- |
| 2 | +title: "Prioritizied Sync" |
| 3 | +description: "In some scenarios, you may want to sync tables using different priorities. For example, you may want to sync a subset of all tables first to log a user in as fast as possible, then sync the reamining tables in the background." |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The general approach is as follows: |
| 9 | + |
| 10 | +1. Define how many priority types you want - typically only two are needed: "high priority" and "the rest" |
| 11 | +2. Create a sync bucket for each priority type |
| 12 | +3. Use [client parameters](/usage/sync-rules/advanced-topics/client-parameters) to control which priorities you want the client to sync |
| 13 | + |
| 14 | +## Example |
| 15 | +Suppose we have two tables: `lists` and `todos` (as per the standard todolist demo app [schema](/integration-guides/supabase-+-powersync#create-the-demo-database-schema)). We want the sync to behave as follows: |
| 16 | + |
| 17 | +1. First, sync all the user's lists, enabling us to render the initial screen in the app |
| 18 | +2. Then, sync the user's todos |
| 19 | + |
| 20 | +Below are the sync rules that will enable this: |
| 21 | + |
| 22 | +```yaml |
| 23 | +bucket_definitions: |
| 24 | + # sync high priority tables first, in this case the user's lists |
| 25 | + high_priority: |
| 26 | + parameters: select id as list_id from lists where owner_id = token_parameters.user_id and (request.parameters() ->> 'high_priority' = 'true') |
| 27 | + data: |
| 28 | + - select * from lists where id = bucket.list_id |
| 29 | + # sync any remaining tables, in this case todo items |
| 30 | + remaining: |
| 31 | + parameters: select id as list_id from lists where owner_id = token_parameters.user_id and (request.parameters() ->> 'remaining_tables' = 'true') |
| 32 | + data: |
| 33 | + - select * from todos where list_id = bucket.list_id |
| 34 | +``` |
| 35 | +
|
| 36 | +It is recommended to set Client Parammeters in the [Diagnostics App](https://github.com/powersync-ja/powersync-js/tree/main/tools/diagnostics-app) to verify functionality at this point: |
| 37 | +
|
| 38 | +<Frame> |
| 39 | + <img src="/images/usage/use-case-prioritized.png"/> |
| 40 | +</Frame> |
| 41 | +
|
| 42 | +If everything checks out, you can then proceed to implement the client parameter switching accordingly in your app. |
0 commit comments