PackagesauditMenu

@intelligo-dev/audit

Append-only audit events for multi-tenant apps, with a Postgres trigger that refuses every update and delete.

Install

Terminal
pnpm add @intelligo-dev/audit drizzle-orm

drizzle-orm is a peer. The audit_events table and its trigger are part of the framework’s schema, which intelligo migrate from @intelligo-dev/cli applies.

Use

TypeScript
import { recordAuditEvent } from "@intelligo-dev/audit";

await recordAuditEvent({
  workspaceId,
  actorId: user.id,
  action: "member.removed",
  resourceKind: "member",
  resourceId: memberId,
  metadata: { role: "admin" },
});

An event with no actorId is a system actor’s — a cron, a webhook, the job runner; actorKind: "support" marks an operator acting on a tenant. outcome is "ok" unless the caller says "failed".

recordAuditEvent never throws — an audit failure must not fail the action it records. recordAuditEventOrThrow exists for the callers where the record is the action: impersonation, a destructive support operation.

queryAuditEvents({ workspaceId, action, resourceKind, resourceId, before, limit }) reads the trail most recent first, paginated by before.

Append-only, in the database

audit_events refuses UPDATE and DELETE at the database level, with one exception: the ON DELETE SET NULL the foreign keys perform, so deleting a workspace does not require deleting its trail.

A workspace’s own security history

Who invited, removed or promoted whom, who left, who took ownership and who deleted the workspace: @intelligo-dev/auth’s team and workspace services say so to a port once the change is made, and recordSecurityEvent writes it as a row of that workspace’s trail. A new app binds the two in its composition root; an existing one adds:

TypeScript
import { recordSecurityEvent } from "@intelligo-dev/audit";
import { setSecurityEventHandler } from "@intelligo-dev/auth";

setSecurityEventHandler(recordSecurityEvent);

The actions are member.invited, invitation.cancelled, member.joined, member.removed, member.role_changed, member.left, workspace.ownership_transferred, workspace.updated and workspace.deleted (SECURITY_EVENT in @intelligo-dev/auth); the chat item adds conversation.shared and conversation.unshared when a public link is made or withdrawn. A row carries ids and roles and no email address. One made while a platform admin acted as the user is a support row with the admin’s id in metadata.impersonatedBy. A failed write never fails the change it describes. A product’s own services emit through the same port with emitSecurityEvent.

Sinks

TypeScript
import { registerAuditSink } from "@intelligo-dev/audit";

registerAuditSink("siem", async (event) => {
  await ship(event);
});

A sink registered from the composition root receives every event after it is durably recorded, with failure isolation: an exporter with an expired credential breaks neither the audited operation nor the other sinks. It is how tamper-evidence, a retention policy or a SIEM export attaches without this package knowing about it.

Entry points

  • @intelligo-dev/audit
  • @intelligo-dev/audit/db

npm · source