Skip to main content

TriggerManagerImpl

Internal Experimental

Implements

Constructors

new TriggerManagerImpl()

new TriggerManagerImpl(options): TriggerManagerImpl

Experimental

Parameters

ParameterType
optionsTriggerManagerImplOptions

Returns

TriggerManagerImpl

Methods

cleanupResources()

cleanupResources(): Promise<void>

Experimental

Returns

Promise<void>


createDiffTrigger()

createDiffTrigger(options): Promise<() => Promise<void>>

Experimental

Parameters

ParameterType
optionsCreateDiffTriggerOptions

Returns

Promise<() => Promise<void>>

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'
}
});

Implementation of

TriggerManager.createDiffTrigger


dispose()

dispose(): void

Experimental

Returns

void


trackTableDiff()

trackTableDiff(options): Promise<TriggerRemoveCallback>

Experimental

Parameters

ParameterType
optionsTrackDiffOptions

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
}
}
});

Implementation of

TriggerManager.trackTableDiff


updateDefaults()

updateDefaults(config): void

Experimental

Parameters

ParameterType
configTriggerManagerImplConfiguration

Returns

void