Query<RowType>
Type Parameters
| Type Parameter |
|---|
RowType |
Methods
differentialWatch()
differentialWatch(options?): DifferentialWatchedQuery<RowType>;
Creates a WatchedQuery which watches and emits results of the linked query.
This query method watches for changes in the underlying SQLite tables and runs the query on each table change. The difference between the current and previous result set is computed. The watched query will not emit changes if the result set is identical to the previous result set.
If the result set is different, the watched query will emit the new result set and emit a detailed diff of the changes via the onData and onDiff listeners.
The deep differentiation allows maintaining result set object references between result emissions.
The DifferentialWatchedQuery#state data array will contain the previous row references for unchanged rows.
Parameters
| Parameter | Type |
|---|---|
options? | DifferentialWatchedQueryOptions<RowType> |
Returns
DifferentialWatchedQuery<RowType>
Example
const watchedLists = powerSync.query({sql: 'SELECT * FROM lists'})
.differentialWatch();
const disposeListener = watchedLists.registerListener({
onData: (lists) => {
console.log('The latest result set for the query is', lists);
},
onDiff: (diff) => {
console.log('The lists result set has changed since the last emission', diff.added, diff.removed, diff.updated, diff.all)
}
})
watch()
watch(options?): StandardWatchedQuery<readonly Readonly<RowType>[]>;
Creates a WatchedQuery which watches and emits results of the linked query.
By default the returned watched query will emit changes whenever a change to the underlying SQLite tables is made. These changes might not be relevant to the query, but the query will emit a new result set.
A StandardWatchedQueryOptions#comparator can be provided to limit the data emissions. The watched query will still query the underlying DB on underlying table changes, but the result will only be emitted if the comparator detects a change in the results.
The comparator in this method is optimized and returns early as soon as it detects a change. Each data emission will correlate to a change in the result set, but note that the result set will not maintain internal object references to the previous result set. If internal object references are needed, consider using Query#differentialWatch instead.
Parameters
| Parameter | Type |
|---|---|
options? | StandardWatchedQueryOptions<RowType> |
Returns
StandardWatchedQuery<readonly Readonly<RowType>[]>