TriggerManager
Experimental Alpha
Methods
createDiffTrigger()
createDiffTrigger(options): Promise<TriggerRemoveCallback>;
Experimental
Creates a temporary trigger which tracks changes to a source table and writes changes to a destination table. The temporary destination table is created internally and will be dropped when the trigger is removed. The temporary destination table is created with the structure:
CREATE TEMP TABLE ${destination} (
operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT,
operation TEXT,
timestamp TEXT,
value TEXT,
previous_value TEXT
);
The value column contains the JSON representation of the row's value at the change.
For DiffTriggerOperation#UPDATE operations the previous_value column contains the previous value of the changed row
in a JSON format.
NB: The triggers created by this method might be invalidated by AbstractPowerSyncDatabase#updateSchema calls. These triggers should manually be dropped and recreated when updating the schema.
Parameters
| Parameter | Type |
|---|---|
options | CreateDiffTriggerOptions |
Returns
Promise<TriggerRemoveCallback>
A callback to remove the trigger and drop the destination table.
Example
const dispose = await database.triggers.createDiffTrigger({
source: 'lists',
destination: 'ps_temp_lists_diff',
columns: ['name'],
when: {
[DiffTriggerOperation.INSERT]: 'TRUE',
[DiffTriggerOperation.UPDATE]: 'TRUE',
[DiffTriggerOperation.DELETE]: 'TRUE'
}
});
trackTableDiff()
trackTableDiff(options): Promise<TriggerRemoveCallback>;
Experimental
Tracks changes for a table. Triggering a provided handler on changes. Uses TriggerManager.createDiffTrigger internally to create a temporary destination table.
Parameters
| Parameter | Type |
|---|---|
options | TrackDiffOptions |
Returns
Promise<TriggerRemoveCallback>
A callback to cleanup the trigger and stop tracking changes.
NB: The triggers created by this method might be invalidated by AbstractPowerSyncDatabase#updateSchema calls. These triggers should manually be dropped and recreated when updating the schema.
Example
const dispose = database.triggers.trackTableDiff({
source: 'todos',
columns: ['list_id'],
when: {
[DiffTriggerOperation.INSERT]: sanitizeSQL`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(someIdVariable)}`
},
onChange: async (context) => {
// Fetches the todo records that were inserted during this diff
const newTodos = await context.withDiff<Database['todos']>(`
SELECT
todos.*
FROM
DIFF
JOIN todos ON DIFF.id = todos.id
`);
// Process newly created todos
},
hooks: {
beforeCreate: async (lockContext) => {
// This hook is executed inside the write lock before the trigger is created.
// It can be used to synchronize the current state of the table with processor logic.
// Any changes after this callback are guaranteed to trigger the `onChange` handler.
// Read the current state of the todos table
const currentTodos = await lockContext.getAll<Database['todos']>(
`
SELECT
*
FROM
todos
WHERE
list_id = ?
`,
['123']
);
// Process existing todos
}
}
});