audit() function

Build the middleware recording every write to the tables it is given.

Signature:

export declare function audit(input: AuditOptions): SqlMiddleware & {
    close(): Promise<void>;
};

Parameters

Parameter

Type

Description

input

AuditOptions

The trail's connection, table config, tables and actor.

Returns:

SqlMiddleware & { close(): Promise<void>; }

Middleware with a close() for teardown.

Remarks

Rows are captured as the database returns them, so the trail holds what was actually written — including values defaulted in SQL — and is inserted once the statement completes.

**Buffered per execution, not per client.** onRow is called from an async generator, so two concurrent statements on one client interleave at every row. A single shared buffer lets one statement's afterQuery flush the other's rows and stamp them with the wrong actor, which on a security trail is the worst failure available. Keying by the plan object confines each statement to its own rows, and a WeakMap drops the buffer of a statement that is never drained — an early break, or a first() — rather than leaking it into whatever flushes next.

The actor is resolved at the **first row**, inside the statement's own async context, for the same reason.

Call close() on the returned middleware when shutting down, or the pool keeps the process alive.

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.