installChangeTriggers() function
Install Postgres triggers that NOTIFY on every row change in the given tables.
Signature:
export declare function installChangeTriggers(client: RawClient, input: ChangeTriggerConfig): Promise<void>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
client |
A client that can execute raw SQL and open a transaction. | |
|
input |
Returns:
Promise<void>
Nothing; it resolves once the triggers match models.
Remarks
Creates one shared plpgsql function and attaches a row-level trigger to each table in models, firing after every insert, update and delete. Each notification is a JSON payload with three keys — table, op (INSERT, UPDATE or DELETE) and row — where row is the new row, or the OLD row for a delete. Listen for them with @imqueue/pg-pubsub, or any LISTEN client.
The whole thing runs in one transaction and is idempotent, so it is safe on every start. It also reconciles in both directions: models is the complete desired set, read against information_schema.triggers, so a table that carries the trigger but is not listed has it dropped. Calling this with an empty or omitted models therefore REMOVES every trigger of that name — it is not a no-op.
Two limits worth knowing. Postgres caps a notification payload at 8000 bytes and raises an error beyond it, so a table with large rows can make its own writes fail — this is unsuitable for wide or blob-bearing tables. And the trigger fires per row inside the writing transaction, so a bulk write produces one notification per row, delivered only if that transaction commits.
Example
await installChangeTriggers(prisma, { models: ['User', 'Order'] });
Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.