audit-domain
Mongoose persistence boundary for the append-only audit log (audit_events collection). The portable schema lives in
models/audit-model.
API
| File | Exports |
|---|---|
| audit.schema.ts | getAuditEventModel(connection, retentionDays?), AuditEventDocument, AuditEventMongooseModel |
| audit.repository.ts | createAuditDomainRepository(connection, logger, options?), AuditDomainRepository |
| audit.middleware.ts | createAuditMiddleware({ 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
| Method | What 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
successrow after the handler, with the actor snapshot (fromprincipal),workspaceId(frominput.workspaceId), the target and changes the contract extracts, the request id, client IP, user agent and the active trace id. - Denied or failed: an
AppErrorwith status 401 or 403 isdenied; anything else isfailed. 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 owndeniedorfailedrow. - Best effort: the middleware's own writes never fail the request; a failure is logged. An explicit
recordthrows, 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