API Reference
Data Audit API
Record and query data change events.
Auditor.RecordDataChange(ctx, entry) → error
Records a single data change. Normally called by ORM adapters, but you can invoke it directly from code paths that bypass the adapter (raw SQL, external imports, maintenance jobs).
_ = auditor.RecordDataChange(ctx, audit.DataEntry{
EntityType: "ledger_entries",
EntityID: "le-42",
Action: audit.ActionCreate,
NewValues: map[string]any{"amount": 500},
})Behavior:
- Skips writing when
DataAudit.Enabled == false. - Skips writing when
EntityTypeis inDataAudit.ExcludeEntities. - Applies diff and field exclusion before persistence.
- Suppresses UPDATE rows where nothing changed after exclusion.
- For DELETEs without captured
OldValues, usesNewValuesas the old state. - Reads
UserFuncforuser_id/user_type. - Uses
entry.TransactionIDif set; otherwise reads it from the context viaaudit.TransactionIDFromContext(ctx).
audit.DataEntry
type DataEntry struct {
EntityType string
EntityID string
Action string
OldValues map[string]any
NewValues map[string]any
Metadata map[string]any
TransactionID string
}| Field | Required | Notes |
|---|---|---|
EntityType | Yes | Table / model name. |
EntityID | Yes | Primary key value. Compound PKs are JSON-encoded arrays. |
Action | Yes | One of the Action* constants. |
OldValues | No | Required for meaningful UPDATE / DELETE audit entries. |
NewValues | No | Required for CREATE / UPDATE entries. |
Metadata | No | Application-specific context (IP, reason, request id, …). |
TransactionID | No | Overrides context-derived transaction ID when set. |
Auditor.Query(ctx, filter) → ([]AuditLog, error)
Fetches data audit records matching a filter. Default order is
created_at DESC.
logs, err := auditor.Query(ctx, audit.DataFilter{
EntityType: "users",
Limit: 50,
})audit.DataFilter
type DataFilter struct {
EntityType string
EntityID string
Action string
UserID string
TransactionID string
DateFrom time.Time
DateTo time.Time
Limit int
Offset int
}All fields are optional. Zero values are ignored by the query builder.
audit.AuditLog
type AuditLog struct {
ID uint64 `json:"id"`
EntityType string `json:"entity_type"`
EntityID string `json:"entity_id"`
Action string `json:"action"`
OldValues json.RawMessage `json:"old_values,omitempty"`
NewValues json.RawMessage `json:"new_values,omitempty"`
UserID string `json:"user_id"`
UserType string `json:"user_type,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
TransactionID string `json:"transaction_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
}Related
- Query & Filters — transaction-level queries and pagination.
- audit_logs schema — table and indexes.