Skip to main content

TriggerDiffHandlerContext

Experimental Alpha

Context for the onChange handler provided to TriggerManager#trackTableDiff.

Properties

PropertyTypeDescription
contextLockContextAlpha Experimental
destinationTablestringAlpha The name of the temporary destination table created by the trigger.
withDiff<T>(query, params?, options?) => Promise<T[]>Alpha Allows querying the database with access to the table containing DIFF records. The diff table is accessible via the DIFF accessor. The DIFF table is of the form described in TriggerManager#createDiffTrigger CREATE TEMP DIFF ( operation_id INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT, operation TEXT, timestamp TEXT, value TEXT, previous_value TEXT ); Note that the value and previous_value columns store the row state in JSON string format. To access the row state in an extracted form see TriggerDiffHandlerContext#withExtractedDiff. Examples --- This fetches the current state of todo rows which have a diff operation present. --- The state of the row at the time of the operation is accessible in the DIFF records. SELECT todos.* FROM DIFF JOIN todos ON DIFF.id = todos.id WHERE json_extract(DIFF.value, '$.status') = 'active' // With operation_id cast as TEXT for full precision const diffs = await context.withDiff<TriggerDiffRecord<string>>( 'SELECT * FROM DIFF', undefined, { castOperationIdAsText: true } ); // diffs[0].operation_id is now typed as string
withExtractedDiff<T>(query, params?) => Promise<T[]>Alpha Allows querying the database with access to the table containing diff records. The diff table is accessible via the DIFF accessor. This is similar to TriggerDiffHandlerContext#withDiff but extracts the row columns from the tracked JSON value. The diff operation data is aliased as __ columns to avoid column conflicts. For DiffTriggerOperation#DELETE operations the previous_value columns are extracted for convenience. CREATE TEMP TABLE DIFF ( id TEXT, replicated_column_1 COLUMN_TYPE, replicated_column_2 COLUMN_TYPE, __operation TEXT, __timestamp TEXT, __previous_value TEXT ); Example SELECT todos.* FROM DIFF JOIN todos ON DIFF.id = todos.id --- The todo column names are extracted from json and are available as DIFF.name WHERE DIFF.name = 'example'