Back to Projects
library

Prisma Audit Log

A zero-overhead Prisma client extension for automatic audit logging — transparently tracks creates, updates, deletes, and upserts with before/after diffs, batch handling, and sensitive field masking.

Prisma Audit Log

Overview

Prisma Audit Log (@explita/prisma-audit-log) is a transparent Prisma Client query extension that brings comprehensive audit logging, regulatory compliance, and historical change tracking to Node.js applications with zero query boilerplate. By mounting .$extends(auditLogExtension({ ... })), every single-row and batch database mutation (create, update, delete, createMany, updateMany, deleteMany, and upsert) is automatically intercepted, diffed, and written to a queryable audit table.

Unlike naive database triggers or logging middleware, Prisma Audit Log captures full historical context. Every operation records the previous snapshot (oldData), the resulting payload (newData), and a strict array of modified column names (changedFields). For complex workflows, upsert queries are dynamically categorized as CREATE_upsert or UPDATE_upsert based on whether the entity was newly created or modified. The serialization engine natively handles Prisma Decimal instances and JavaScript BigInt values (converting them to strings to prevent JSON serialization errors), while silently skipping updates where no fields were actually changed.

The extension is built for compliance (SOC 2, GDPR, HIPAA) with strict noise-reduction defaults: updates touching only timestamps (updatedAt or updated_at) are silently bypassed, self-referential logging on the AuditLog table is automatically prevented, and a dynamic skip callback (({ model, operation, args }) => boolean | Promise<boolean>) allows developers to conditionally exclude internal operations, sessions, or system updates. For privacy and data governance, sensitive fields are redacted using maskFields (with customizable maskValue, defaulting to [REDACTED]), granular per-model fieldFilters (whitelisting via include or blacklisting via exclude), and payload truncation limits (maxStringLength, maxArrayLength, maxPayloadBytes).

Every audit entry can be enriched with contextual metadata via getContext(), seamlessly binding authenticated user IDs (userId), tenant identifiers, client IP addresses (ipAddress), user agent strings, and custom request telemetry. Audit records are persisted using high-performance batch inserts (createMany) with an automatic fallback to single-row writes for resilient reliability. In addition, an optional logger callback allows applications to stream structured AuditLog[] records directly to external observability pipelines such as Datadog, AWS CloudWatch, or OpenTelemetry.

Tech Stack

backend

TypeScriptNode.jsPrisma@explita/prisma-audit-log

Tags

#TypeScript#Prisma#Audit Log#Database#Security#Compliance#NPM Package#Backend#Library