Skip to content

Automatic CUD Tracking

Automatic tracking works via Prisma $extends. When you use the extended client for business writes, create, update, delete, upsert, and batch operations are automatically tracked.

Configuration

Tracking behavior is configured through createAuditExtension(options):

OptionTypeDefaultDescription
trackedModelsstring[]Whitelist of Prisma model names to track
ignoredModelsstring[]Blacklist (used when trackedModels is not set)
sensitiveFieldsstring[][]Fields to mask as [REDACTED] in diffs
primaryKeyRecord<string, string>{ *: 'id' }Map of model name to primary key field name

Transaction Model

PathCaller tx participationAudit insert
Automatic tracking (extension)Yes — query(args) joins caller's $transactionBest-effort — runs after business write, warns on failure
Manual logging (log(input, tx))Yes — when tx providedParticipates in provided transaction
Manual logging (log(input))NoIndependent write via base client

The automatic extension uses Prisma's query(args) callback, which preserves the caller's transaction context. The audit insert runs separately via the base client and does not block or fail the business operation. If audit insert fails, a warning is logged.

Decorators

Apply to individual handlers or entire controllers:

typescript
@NoAudit()      // Skip audit tracking for this route or controller
@AuditAction('user.role.changed')  // Override auto-generated action name

Multi-Tenancy

If @nestarc/tenancy is installed, tenant_id is automatically included in all audit records and query filters.

ScenarioBehavior
Not installedtenant_id is null, library works normally
Installed, context availabletenant_id auto-injected
Installed, context failsWarning logged, tenant_id falls back to null
tenantRequired: true + context failslog() and query() throw an error

Released under the MIT License.