Documentation
¶
Overview ¶
Package bunguard integrates sqlguard with bun (github.com/uptrace/bun).
Analysis is driven by the single shared sqlguard core (middleware.Guard), so redaction-by-default, stable fingerprints, the pluggable real-grammar parser, slow-query timing and N+1 detection behave identically to the database/sql driver wrapper, pgxguard and gormguard. There is no parallel option surface — configure with the standard middleware options:
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bunguard.New(
middleware.WithSlowQueryThreshold(500*time.Millisecond),
middleware.WithN1Detection(10, time.Second),
))
bun exposes the final rendered SQL and a start timestamp on the QueryEvent in its AfterQuery hook, so this uses the explicit Check+CheckLatency pair (matching gormguard) rather than middleware.Guard.Observe: static rules run on every query, latency is reported only on success.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type QueryHook ¶
type QueryHook struct {
// contains filtered or unexported fields
}
QueryHook implements bun.QueryHook and drives every traced statement through the shared sqlguard analysis core.
func New ¶
func New(opts ...middleware.Option) *QueryHook
New creates a new sqlguard bun query hook. It accepts the standard sqlguard middleware options (WithAnalyzer, WithReporter, WithSlowQueryThreshold, WithParser, WithN1Detection, …) — the same option set the database/sql driver wrapper, pgxguard and gormguard use, so there is no parallel configuration surface to drift.
func (*QueryHook) AfterQuery ¶
func (h *QueryHook) AfterQuery(_ context.Context, event *bun.QueryEvent)
AfterQuery implements bun.QueryHook. event.Query holds the rendered SQL.
func (*QueryHook) BeforeQuery ¶
BeforeQuery implements bun.QueryHook. bun stamps event.StartTime itself before invoking the hook, so there is nothing to stash here.