bunguard

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 4 Imported by: 0

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

func (h *QueryHook) BeforeQuery(ctx context.Context, _ *bun.QueryEvent) context.Context

BeforeQuery implements bun.QueryHook. bun stamps event.StartTime itself before invoking the hook, so there is nothing to stash here.

func (*QueryHook) ResetN1

func (h *QueryHook) ResetN1()

ResetN1 clears N+1 tracker state. Call it at a per-request boundary (e.g. end of an HTTP handler) to scope N+1 detection to one unit of work. No-op unless WithN1Detection was passed to New.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL