Documentation
¶
Overview ¶
Package bun is the Bun adapter for go-audit. It attaches a QueryHook that captures INSERT / UPDATE / DELETE statements and records them via the Auditor.
On UPDATE / DELETE the hook performs a pre-query SELECT to snapshot the affected rows, so audit logs carry both old_values and new_values (the snapshot is diffed against the post-state for UPDATE, matching the GORM adapter). The snapshot strategy is, in order:
- If the caller's Model has primary-key values set (e.g. NewUpdate().Model(&x).WherePK() or Model(&slice).Bulk()), SELECT by PK.
- Otherwise, parse the rendered WHERE clause out of the statement and run SELECT * FROM <table> WHERE <same clause>.
Set DataAuditConfig.SkipOldValues = true to skip the pre-query SELECT.
Known limitation: the snapshot SELECT runs against the base *bun.DB, not the current *bun.Tx, so uncommitted rows written earlier in the same transaction are not visible. For strict tx-local snapshots, load rows explicitly before the write, or use the GORM adapter.
Error handling: bun.QueryHook.AfterQuery runs after the statement has already executed and cannot change the error Bun returns to the caller (Bun copies its local err into the event and then returns that same local value), so ErrorFailLoud cannot abort an already-executed Bun query. Instead, when the Auditor returns an error from RecordDataChange — which it does under ErrorFailLoud — the hook passes it to the handler configured via WithErrorHandler. The default handler logs through the Auditor's Config.Logger (log.Printf when nil). Under ErrorFailSilent the Auditor logs and swallows the error itself, so the handler is never invoked.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶ added in v1.1.1
type Option func(*hook)
Option configures the hook installed by Register.
func WithErrorHandler ¶ added in v1.1.1
WithErrorHandler sets the function invoked when the Auditor returns an error from RecordDataChange, i.e. an audit-store failure under ErrorFailLoud. Bun's AfterQuery cannot alter the result of the statement that already executed, so this handler is the only place a Bun write's audit failure can be observed: use it to raise a metric, trip a health check, or mark the surrounding transaction for rollback. The default handler logs via the Auditor's Config.Logger (log.Printf when nil). Under ErrorFailSilent the Auditor already logs and returns nil, so fn is never called. A nil fn keeps the default.