Sync Scopes
How to sync only part of a table per screen using channel scopes instead of pulling everything to the device.
How to sync only part of a table instead of all of it. Read sync-overview.md
first.
The choice
You decide, per channel, how much data lives on the device:
- Whole-app sync (the default). A channel with no scope pulls each of its tables in full into local SQLite. Everything is offline-first and instant. Right for most apps, up to a few thousand rows a table.
- Per-screen / scoped. A channel with a scope pulls only the rows in the current scope (one screen's worth), spanning all the channel's tables. Right when a table is large or the app browses one thing at a time. This pairs naturally with the HTTP fallback: while degraded you want to pull one screen, not a whole table.
The decision is made per channel by the client's pull planner: a channel with no
activeScopes pulls each table whole (the sync unit is the table); a channel with
activeScopes set pulls once per active scope id (the sync unit is the scope).
activeScopes: [] means "scoped, but nothing in view right now" → zero pulls,
which is different from leaving it unset.
The application owns scope membership. The library never computes which rows
are in a scope. It sends the scope and scope_id you supply, and the server
returns only rows whose scope_ids column (a Postgres text[]) contains that id.
A row with no scope_ids comes back on an unscoped pull but never on a scoped one.
A scope only narrows a pull: the member's ownership filter still applies, so a
scope can never reach rows the member could not already read.
Oalo app tables are not scopable yet. The tables Oalo creates for an app have
no scope_ids column, and sync writes never set one. A scoped pull against them
fails (pull_failed over HTTP, an error on the socket) instead of returning a
screen's worth of rows. Until that lands, use the on-demand fetch below for
per-screen loading in an Oalo app.
The current API (and a wrinkle)
The client has two different scope mechanisms. Use the first one for real wire scoping.
1. activeScopes on a channel (the real, wire-level scoping)
Set activeScopes (a list of scope ids) and optional scopeKind on the channel.
Scoped pulls then carry scope + scope_id on the wire and the server filters.
- TypeScript: pass
activeScopesin theChannelConfig, or change it at runtime withclient.updateActiveScopes(topic, scopeIds)(a declarative full-set replace that re-maps channels and triggers a scoped sync). Evict a scope's cursor withclient.resetScopeCursor(scopeId). - Dart: set
SyncChannel.activeScopes/scopeKind. Dart has noupdateActiveScopesorresetScopeCursor, so you reconstruct the channel to change it.
2. syncScope(...) / useSyncScope(...) (the older, name-based shim)
client.syncScope({ name, tables, parameter? }) (TS) and the React
useSyncScope(name, parameter?) hook dedupe by scope key and then just call
sync() and refresh the listed tables. They do not put scope_id on the wire:
sync() is an ordinary full sync of every channel. useSyncScope passes empty
tables, so it refreshes no queries at all.
Dart's syncClient.syncScope(name:, tables:, parameter:) is different. It streams
a snapshot of the listed tables with scope = name and scope_id = parameter on
the wire, so it is a real scoped pull, with the same table requirement as above.
Practical guidance: for genuine bandwidth-limiting, per-screen sync, use
activeScopeson tables that have ascope_idscolumn. Treat the TSsyncScope/useSyncScopeas a legacy "sync once and invalidate these tables" helper, not as wire scoping. The React hook alone never leads you toactiveScopes, which is why this page spells it out.
On-demand fetch is the other per-screen tool
Independent of channel scoping, you can pull exactly what a screen needs with
fetchPage (a page) and ensureRow / ensureRows (by id). This is the simplest
way to "pull one screen at a time," needs no scope wiring, and works in any
transport mode. See sync-api-reference.md and sync-http-fallback.md. Many apps
combine both: whole-app sync the small tables, on-demand the big ones.
Known gaps
- The two scope models (
activeScopesvssyncScope/useSyncScope) overlap and are easy to confuse. - TS
syncScopeand DartsyncScopeshare a name but not a behavior: only the Dart one scopes the wire. useSyncScoperuns a full sync and refreshes nothing, so avoid that path.- Oalo app tables have no
scope_idscolumn, so wire scoping does not work on them yet. UsefetchPageandensureRows.
Sign up, install the CLI, and deploy your repo in a few minutes.