Agent Guide
This guide targets published @nestarc/audit-log@0.9.0. Use the installed package declarations and 0.9.0 release source as the version boundary. Start with the Quick Start, Incremental Adoption, or SQL Server guide.
- Require Node.js
^22.23.4 || ^24.22.1, NestJS 10/11/12.0.1+, and compatible Prisma 5/6/7 dependencies. Use matching Prisma CLI/client/adapter versions. Pass the generated{ Prisma }namespace for Prisma 7 at module and extension boundaries. - Choose the provider explicitly when needed: omitted
providerselects PostgreSQL. SQL Server supports manual logging, managed manual transactions, andquery()/getById(). It does not support automatic tracking, Outbox delivery, scan/export, retention, partitions, GIN indexes, or rule enforcement. The pinned SQL Server validation stack is SQL Server 2025 with Prisma 7.10.0. - Start with one manual event or one PostgreSQL tracked model. Use
defineAuditConfig()to share actor, tenant, table, provider, and masking settings. Put shared fields only inshared, andstrictManualValidation,maxMetadataBytes, andonAuditOperationinmodule. Provideprismaat module registration; omitextensionfor manual-only use. The factory performs no database work. - Create audit storage in an explicit migration with a setup identity. Keep the base client for audit storage. On PostgreSQL, use
createAuditedClient()and atrackedModelsallowlist for automatic writes, withconsistency: 'atomic-required'andwithAuditTransaction(). Caught automatic audit failures still prevent helper commit.best-efforthas independent inserts. - For manual events, use
audit.withManualTransaction(base, async ({ tx, log }) => ...)when caught audit failures must prevent commit. Await the bound writer; use itstxfor all business writes. Already-started writes settle before completion, expired writers reject, and nested managed helpers are unsupported. The helper cannot detect missing events or enlist a captured base client. Without delivery, caller-ownedlog(input, tx)remains available; its errors must escape the callback. Configured delivery rejects publiclog()calls, even with a managedtx. - Manual validation defaults to off. Before enabling it, review identifiers and JSON metadata.
maxMetadataBytesmeasures final redacted JSON in UTF-8 bytes, including context metadata; oversize events reject rather than truncate. HandleAuditInputError.codeand.fieldwithout logging raw event values. Catching these errors inside the managed helper still prevents commit. - Use
actorExtractionStage: 'interceptor'when a Guard populates identity. Authenticate and authorize in the host. OptionalactorRequiredneeds non-blank string IDs for users, API keys, and workers. Workers useAuditContext.runAs()and establish tenant context separately. Automatic exclusions remain bypasses; explicit manual logs still enforce actor policy. - Use explicit operations for tracked children. Atomic nested-write checks include untracked parents and intermediate relations. Atomic
createMany,updateMany,createManyAndReturn, andupdateManyAndReturnreject for tracked models; use sequential single-record calls.deleteManyhas a per-record cap. See Automatic CUD Tracking. - Authorize history access before selecting tenant scope. Match action and source: automatic
Invoice.*versus manualinvoice.approved. SQL Server cursors retain full timestamp precision; keep them opaque and never reuse them across providers. Read credentials need SELECT privileges. getCapabilities()reports configured provider APIs without database I/O; it does not prove connection, schema, or permissions. The audited client'sgetAuditCapabilities()reports the separate automatic consistency/lifecycle contract.onAuditOperationomits event contents; a successful log reports INSERT completion, while managed transactions have a final outcome. Filter detailedonAuditErrordata before forwarding it to telemetry.- PostgreSQL exports require explicit scope.
after === untilcompletes without replay. Durable streams may miss late commits; use entry-ID deduplication and CDC or reconciliation for historical capture. New opted-in producer writes can use Atomic Outbox Delivery without relying on an audit timestamp checkpoint. Load required checkpoints before retention; missing checkpoints must stop pruning. - For tenant-scoped PostgreSQL lifecycle writes, pin tenancy 0.16.4, audit-log 0.8.0, and soft-delete 0.7.6 with strict peer installation. This is a separate 0.8.0 recipe: soft-delete 0.7.6 excludes audit-log 0.9.0. Upgrade soft-delete from 0.7.5 or earlier first; this composition supports NestJS 10/11 and Prisma 6/7. Create a dedicated audit-log → soft-delete client, inject it into
SoftDeleteService, and passtenancyTransactionCallback(tenancy, work)directly to itswithAuditTransaction(). Lifecycle recipe - For new PostgreSQL audit delivery, use
delivery: createOutboxAuditDelivery(emitter)with exact Outbox 0.4.1 and audit-log 0.9.0. Apply both schemas before enabling it. Require the managed manual writer oratomic-requiredautomatic tracking on the same transaction; staging failure prevents commit even when caught. Keep network delivery in an Outbox worker. The fixed summary excludes changes, arbitrary metadata, and actor IP. Consumers deduplicate by tenant/audit ID; no historical backfill, commit ordering, or exactly-once guarantee is added.
Verify a committed write, rollback, actor and tenant values, redaction, and an authorized history query. Test missing-actor and enabled validation/size failures, including caught failures in the managed helper. Keep setup/maintenance credentials separate from the request-serving runtime. SQL Server inspection can have incomplete metadata and never proves trigger enforcement; verify actual permissions and commit/rollback behavior. Read indexes require an explicit migration. For delivery, also verify enqueue failure, reversed commit order, producer loss after commit, and consumer deduplication. Distinguish the released source RLS regression from the runnable Outbox example's tenant-attribution checks; see verification scope.