Listoca

Offline data and sync

The local database, row level security, sync streams, refused changes and the no-account mode.

Every feature that stores data is described in three places, which a test keeps in agreement:

  1. Postgres (supabase/migrations/*.sql): the table, its row level security policies, and alter publication powersync add table ….
  2. A sync stream (powersync/sync-config.yaml): which rows and columns each device receives.
  3. The local table (packages/sync/src/schema/tables.ts): what the device stores.

packages/sync/src/schema.test.ts fails when a stream selects a column the local table lacks, or the database doesn't have.

Row level security

Every table in public has row level security on from the migration that creates it. A list's tasks, for example:

create policy "members read tasks" on public.tasks for select to authenticated
  using (
    case when list_id is null then owner_id = (select auth.uid())
    else (select private.is_list_member(list_id)) end
  );
create policy "tasks.write adds tasks" on public.tasks for insert to authenticated
  with check (
    case when list_id is null then owner_id = (select auth.uid())
    else (select private.has_list_permission(list_id, 'tasks.write')) end
  );

The device never decides who owns a row: a trigger sets owner_id from the token on insert and keeps it on update. Policies are tested from the side that must be refused (*.db.test.ts, see testing).

Sync streams

lists:
  auto_subscribe: true
  query: SELECT id, owner_id, name, description, color, icon, paused_at, created_at, updated_at FROM lists WHERE id IN (SELECT list_id FROM list_members WHERE user_id = auth.user_id())

A stream filters by the signed-in user (auth.user_id() is the token's subject) or selects rows that are the same for everyone, and lists its columns. Streams without auto_subscribe are subscribed by the screen that needs them. Installed apps keep asking for the streams they know: add a new stream, release, then remove the old one.

The local database

packages/sync opens PowerSync's SQLite with Drizzle on top. Reads are query builders that screens run as live queries; writes return at once:

const { drizzle } = useDatabase();
const writer = useWriter();
const { data: lists } = useLiveQuery(listsWithCounts(drizzle, userId));
await createList(drizzle, writer, {
  name: "Groceries",
  description: "",
  color: "green",
  icon: "cart",
});

A live query runs after the screen's first render, so that render has no rows. Where a press opens a screen, the press can read the screen's queries first with preloadLiveQueries (packages/sync/src/preload.ts), and the screen starts with those rows. Home does this for the list screen (usePreloadTaskList); the screen and the preload build their queries with the same function, taskListQueries, since a preloaded result is found by its SQL and parameters.

Lists, tasks, steps and marks live in two homes; the queries route each write (Lists and tasks).

Ids are uuids made on the device, so a row exists before it reaches the server. The server sets owner_id and updated_at.

Uploads and refused changes

PowerSync queues every local write. When online and signed in, the connector (packages/sync/src/connector.ts) sends each transaction to Postgres. How a table's writes are sent is its upload rule (upload-rules.ts): row (insert, update, delete), update (the server makes the rows), or readonly.

Network errors and server errors retry. A write the server refuses (row level security 42501, a constraint 23…, bad data 22…) can't succeed on retry: it is completed, recorded in the device's sync_rejections table with what the user wrote, and the next sync brings back the server's version. The app shows these under Settings → Sync.

Conflicts resolve by upload order: the last write to reach the server wins, per row.

Without an account

With sync.account: "optional" (apps/native/src/config.ts), the app works without signing in. The tables in localOnlyTables then have a local-only twin that holds the rows under the table's name. Signing in moves them into the synced table, where they upload. Signing out lets the user keep a copy on the device or remove it. With "required", the sign-in screen comes first.

Changing the schema

The add-a-synced-table skill (.agents/skills/add-a-synced-table) walks through it; debugging-sync helps when rows don't arrive.

On this page