Tracevault
Open source · MIT · Node.js + PostgreSQL

Add traceability and compliance to your SaaS in minutes.

Open-source audit library for Node.js and PostgreSQL. Without weighing down your database, with strict TypeScript types.

npm install tracevault pg
emit.ts
import { startTracevault } from "tracevault"

const audit = await startTracevault({
  driver: "postgres",
  connectionString: process.env.DATABASE_URL_WRITE!,
  defaultScope: "default",
  scopes: { default: { tableName: "audit_logs" } },
  bootstrap: { ensureSchema: true },
  maskFields: ["password", "token"],
})

await audit.emit({
  event: "product.price.updated",
  actor:  { id: "user_123",    type: "user" },
  target: { id: "product_456", type: "product" },
  data:   { oldPrice: 120, newPrice: 150, currency: "UYU" },
  meta:   { source: "admin-panel" },
})
Before / after

Stop hand-rolling audit inserts.

The same business fact, written two ways. One is a liability in a security review. The other is a typed event you can query later.

The headache

Ad-hoc SQL, stringified payloads, and secrets that land in the log because nobody owns the write path.

controller.ts
await db.query(
  `INSERT INTO audit_logs (event, actor_id, payload)
   VALUES ($1, $2, $3)`,
  [
    "invoice.sent",
    req.user.id,
    JSON.stringify({
      ...req.body,
      card: req.body.card,
      token: req.body.token,
    }),
  ],
);

The Tracevault standard

One emit() call. Actor, target, and payload stay typed. Masking and persistence are the library’s job.

audit.ts
await audit.emit({
  event: "invoice.sent",
  actor:  { id: req.user.id, type: "user" },
  target: { id: invoice.id, type: "invoice" },
  data:   { amount: invoice.total, currency: "USD" },
  meta:   { source: "billing-api" },
});
Why teams adopt it

An audit trail your reviewers can follow.

Built for B2B SaaS that needs evidence of who changed what — without turning the product into an observability platform.

SOC 2 & compliance ready

A stable event shape — actor, target, timestamp, correlation — so security reviews and customer questionnaires have a place to look.

Performance first

Sync when the write must land. Async when it should leave the request path. An in-process queue, not another service in the critical path.

Developer experience

Native TypeScript. You name the events. Validation fails fast, and audit.query reads the same tables you already own.

How it works

Five steps from zero to stored events.

The surface area stays small: one app, explicit scopes, predictable writes, flexible schema setup, and your choice of read path.

  1. 01

    Start the app

    await startTracevault({ scopes, defaultScope, … }). Optional readConnectionString or readPool for production read roles.

  2. 02

    Name your scopes

    List every physical table in scopes once. Use audit.emit on the default scope or audit.getScope("users") for other domains.

  3. 03

    Emit custom events

    Call emit() or emitDiff(). Correlation helpers keep request traces aligned across emits.

  4. 04

    Schema your way

    Let ensureSchema bootstrap tables, or apply sql/001–003 / generateInitSql yourself when migrations own DDL.

  5. 05

    Read your way

    Use audit.query or getScope(name).query — or pg, Drizzle, Prisma, Knex, raw SQL for everything else.

Examples

Core write API, in three snippets.

Named scopes, correlation helpers, schema bootstrap, and audit.query live in the docs — this section is the smallest happy path.

Start the app

await startTracevault with scopes and optional readConnectionString. maskFields applies recursively to data, meta, before and after.

audit.ts
import { startTracevault } from "tracevault"

const audit = await startTracevault({
  driver: "postgres",
  connectionString: process.env.DATABASE_URL_WRITE!,
  readConnectionString: process.env.DATABASE_URL_READ,
  defaultScope: "default",
  scopes: {
    default: { tableName: "audit_logs" },
    users: { tableName: "audit_user_events" },
  },
  bootstrap: { ensureSchema: true },
  maskFields: ["password", "token", "pin"],
  defaultMode: "sync",
  environment: process.env.NODE_ENV,
})

Emit a custom event

Only event is required. actor, target, data, meta, correlationId and requestId are optional — use what makes sense.

login.ts
await audit.emit({
  event: "auth.login.succeeded",
  actor:  { id: "user_123", type: "user" },
  meta:   { ip: "127.0.0.1", userAgent: "curl/8" },
  correlationId: "req_abc",
})

Emit an object change

emitDiff is sugar around emit. It computes a shallow field diff and persists { before, after, diff } as the event payload.

update.ts
await audit.emitDiff({
  event: "product.updated",
  actor:  { id: "user_123",    type: "user" },
  target: { id: "product_456", type: "product" },
  before: { name: "Café", price: 120 },
  after:  { name: "Café", price: 150 },
})

// Persisted data:
// {
//   "before": { "name": "Café", "price": 120 },
//   "after":  { "name": "Café", "price": 150 },
//   "diff":   { "price": { "before": 120, "after": 150 } }
// }

For named scopes, schema options, and audit.query, see Documentation.

Philosophy

A library, not a framework.

Most audit tools push a worldview. Tracevault stays out of your way and gives you a reliable place to land events you already know how to name.

  • 01

    Does not impose an event catalog.

  • 02

    Does not require an ORM.

  • 03

    Does not try to be a framework.

  • 04

    Stays minimal, typed, and predictable.

  • 05

    Keeps audit.query narrow on purpose — no query DSL.

Tracevault does not define your event catalog. It gives you a consistent, reliable way to store your custom audit events. When you need indexed reads, audit.query stays deliberately narrow — the rest belongs in SQL you already trust.
Upgrading

Migrating from 0.x

1.x unifies writes and reads on one app object. The schema and event shape are unchanged — mostly import and config renames.

  • createTracevault(config)await startTracevault({ scopes, defaultScope, … })
  • root.scope({ tableName })scopes at startup + audit.getScope("name")
  • import from tracevault/queryaudit.query and getScope(name).query on the same app
  • Manual SQL onlybootstrap.ensureSchema: true by default (or keep your migrations)

Full release notes and breaking changes: CHANGELOG. Next: Install and Writing.