Efficiently get row changes using trigger-based table diffs (JS)
db.triggers
APIs are experimental. We’re actively seeking feedback on:writeLock
during processing (only a single writeLock
is allowed). Differential watches run on read connections and re-query/compare results on each change (often concurrent on some platforms).data
column).SQLite cannot attach triggers to INSERT/UPDATE/DELETE operations on views — triggers must target the underlying base tables. The db.triggers
API handles these details for you:source
; PowerSync resolves and targets the corresponding underlying table internally.withExtractedDiff(...)
helper.trackTableDiff
. It wraps the lower-level trigger setup, automatically manages a writeLock
during processing, exposes a DIFF
table alias to join against, and cleans up when you call the returned stop()
function. Think of it as an automatic “watch” that processes diffs as they occur.
when
when
parameter lets you add conditions that determine when the triggers should fire. This corresponds to a SQLite WHEN clause in the trigger body.
NEW
for INSERT
/UPDATE
and OLD
for DELETE
.data
column; the row identifier is id
.json_extract(NEW.data, '$.column')
or json_extract(OLD.data, '$.column')
to reference logical columns.'TRUE'
to track all changes for a given operation.when
are embedded directly into the SQLite trigger creation SQL. Sanitize any user‑derived values. The sanitizeSQL
helper performs some basic sanitization; additional sanitization is recommended.trackTableDiff
unless you need to manage lifecycle and locking manually (e.g., buffer diffs to process them later). Note that since the table is created as a temporary table on the SQLite write connection, it can only be accessed within operations performed inside a writeLock.