Skip to Content
gentlemans-stack
ReferenceDomains

audit-domain

Mongoose persistence boundary for the append-only audit log (audit_events collection). The portable schema lives in models/audit-model.

API

FileExports
audit.schema.tsgetAuditEventModel(connection, retentionDays?), AuditEventDocument, AuditEventMongooseModel
audit.repository.tscreateAuditDomainRepository(connection, logger, options?), AuditDomainRepository
audit.middleware.tscreateAuditMiddleware({ auditRepository, service }), AuditRecorder, AuditRecordOptions, resolveAuditClientIp

Rows are never updated, so the schema has no updatedAt, and the repository has no update or delete method.

Methods

MethodWhat it does
record(event, { session? })Validates the event, drops changes fields the action does not allow (AUDIT_CHANGE_FIELDS) and stores one row. Pass the caller's ClientSession so the row commits or aborts with the change it describes.
list({ workspaceId, action?, actorId?, cursor?, limit? })A workspace's events, newest first, as { items, nextCursor? }. limit defaults to 50 (max 100). A malformed cursor is ignored.

options.retentionDays adds a TTL index on at; without it every row is kept. The model is built once per connection, so the option only takes effect the first time the repository is created on that connection.

Recording from a service

createAuditMiddleware is an oRPC middleware for procedures that declare .meta({ audit: { action, target?, changes? } }) in their contract. Attach it after the authenticate middleware and before the intent guard, so a denied attempt is recorded too.

  • Success: it writes one success row after the handler, with the actor snapshot (from principal), workspaceId (from input.workspaceId), the target and changes the contract extracts, the request id, client IP, user agent and the active trace id.
  • Denied or failed: an AppError with status 401 or 403 is denied; anything else is failed. The error is rethrown.
  • Explicit record: a handler calls context.audit.record({ changes, session, target, workspaceId }) when the row must join a transaction. The middleware then skips its own success row; a later failure still gets its own denied or failed row.
  • Best effort: the middleware's own writes never fail the request; a failure is logged. An explicit record throws, so a transaction that depends on it aborts.
  • The first run owns the recorder; a nested run (a router wrapping already-guarded procedures) passes through.

Indexes

  • { workspaceId, at: -1 }: the activity list.
  • { workspaceId, action, at: -1 }: the list filtered by action.
  • { "actor.id", at: -1 }: the list filtered by actor.

Tests

audit.repository.spec.ts runs against a one-node in-memory replica set (transactions need one). It covers record (serialization, the change allow-list, commit and abort with a session), list (order, workspace isolation, filters, cursor paging) and that only record and list exist.

Commands

pnpm nx run audit-domain:typecheck   # type-check the package
pnpm nx run audit-domain:test        # run the Vitest suite

On this page