Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
Binary file added images/usage/use-case-prioritized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@
"usage/use-case-examples/full-text-search",
"usage/use-case-examples/infinite-scrolling",
"usage/use-case-examples/offline-only-usage",
"usage/use-case-examples/postgis"
"usage/use-case-examples/postgis",
"usage/use-case-examples/prioritized-sync"
]
},
{
Expand Down
42 changes: 42 additions & 0 deletions usage/use-case-examples/prioritized-sync.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: "Prioritized Sync"
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 remaining tables in the background."
---

## Overview

The general approach is as follows:

1. Define how many priority types you want - typically only two are needed: "high priority" and "the rest"
2. Create a sync bucket for each priority type
3. Use [client parameters](/usage/sync-rules/advanced-topics/client-parameters) to control which priorities you want the client to sync

## Example
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)). Further, suppose we want the sync priority to behave as follows:

1. First, sync all the user's lists, enabling us to render the initial screen in the app
2. Then, sync the user's todos

Below are the sync rules that will enable this:

```yaml
bucket_definitions:
# always sync high priority tables (first), in this case the user's lists
high_priority:
parameters: select id as list_id from lists where owner_id = token_parameters.user_id
data:
- select * from lists where id = bucket.list_id
# sync any remaining tables, in this case todo items
remaining:
parameters: select id as list_id from lists where owner_id = token_parameters.user_id and (request.parameters() ->> 'remaining_tables' = true)
data:
- select * from todos where list_id = bucket.list_id
```

It is recommended to set Client Parameters in the [Diagnostics App](https://github.com/powersync-ja/powersync-js/tree/main/tools/diagnostics-app) to verify functionality at this point:

<Frame>
<img src="/images/usage/use-case-prioritized.png"/>
</Frame>

If everything checks out, you can then proceed to implement the client parameter switching accordingly in your app.