Litestone guide / Protected data
The audit trail
A driver trail database records who changed what. @@trail on a model files one entry per row written, with the before and after image and the actor, and protected fields are redacted on the way in.
database audit {
path "./audit/"
driver trail
retention 90d
}
model Vault {
id Int
name String
apiKey String?
(audit)
(0)
}
const sys = db.asSystem()
await sys.vault.create({ data: { id: 7, name: 'prod', apiKey: 'sk-1' } })
await sys.vault.update({ where: { id: 7 }, data: { apiKey: 'sk-2' } })
await new Promise(r => setTimeout(r, 0)) // an entry lands one tick after its write
const entries = await sys.auditTrail.findMany({ where: { model: 'Vault', operation: 'update' } })
const text = JSON.stringify(entries)
if (/sk-[12]/.test(text) || !/\[redacted\]/.test(text) || !/prod/.test(text)) throw new Error('the secret must be redacted and the name kept')
The trail records that a protected field was written, by whom and when, never what it holds. A trail database synthesizes its own model, AuditTrail, read as db.auditTrail and open to asSystem() only.
What else it can do
@@trail(audit)logs writes, and@@trail(audit, reads: true)logs reads as well@trail(audit)on a field logs that field's reads and writes, withreads: falseorwrites: falseto narrow it@encrypted,@guarded,@hashedand@secretlog as[redacted],@personalas[personal]and@opaqueas[opaque:<digest>]nullis kept as it is, so a secret going fromnullto set stays visible- A single-row write carries
beforeandafterimages, and a bulk write files one entry per row with the ids alone - Every write path is logged, including
upsertMany,removeandrestore - An entry carries
operation,model,recordId,field,transition,actorId,actorTypeandmeta - A refused access files a
deniedentry, so a policy that filtered to nothing can be told from a real miss onLog(entry, ctx)overrides the actor and addsmeta, andlogContextsuppliesip,userAgentandcorrelationId- The history of one record is a plain
where: { model, recordId }, and a$rawpredicate is refused - A reader that is not the system is declared on the host's own
AuditTrailmodel with its own gate and policy retention 90dprunes on startup, anddb.asSystem().$retain()runs the same pass on demand@@anonymousrefuses a trail, or a writer stamp, on rows nobody may attributedb.$audit()records an event that is not a write
In depth: Trail, § Protected fields are redacted, § Personal data, § onLog, § Retention, Guide: Audit Trail