Skip to main content
Version: Next (unreleased)

Integrations Overview

The runtime middleware wraps the database/sql driver, which already covers every library built on database/sql — sqlc, ent, sqlx, GORM, pgx-stdlib. The integrations exist for two reasons:

  1. The library bypasses database/sql. Native pgx / pgxpool talk to PostgreSQL directly. pgxguard hooks pgx's tracer seam instead.
  2. You want a handle. The driver wrapper hands back a bare *sql.DB, so there is nothing to call ResetN1() on. Every integration holds the guard and exposes ResetN1(), which lets you scope N+1 detection to a request.

All six are built on the same exported middleware.Guard, so they inherit redaction-by-default, fingerprints, the parser seam, config, de-duplication and the analysis cache — and they all take the same middleware.Option set. There is no second surface to learn.

Coverage matrix

ModuleHooksCoversInstall
gormguardgorm.Plugin callbacks on all six chainsCreate / Query / Update / Delete / Row / Rawgo get github.com/KARTIKrocks/sqlguard/integrations/gormguard
sqlxguardWrapper around *sqlx.DBSelect, Get, Queryx, NamedExec (+ Context), Query, Execgo get github.com/KARTIKrocks/sqlguard/integrations/sqlxguard
pgxguardpgx.QueryTracer + pgx.BatchTracerQuery, QueryRow, Exec, SendBatch on pgx.Conn and pgxpool.Poolgo get github.com/KARTIKrocks/sqlguard/integrations/pgxguard
bunguardbun.QueryHookEvery query bun rendersgo get github.com/KARTIKrocks/sqlguard/integrations/bunguard
xormguardxorm contexts.HookEvery statement xorm executesgo get github.com/KARTIKrocks/sqlguard/integrations/xormguard
entguardDecorates ent's dialect.DriverExec, Query, and transactions it opensgo get github.com/KARTIKrocks/sqlguard/integrations/entguard

Each is its own Go module, so its ORM dependency enters your build only when you import it. All nine modules in the repository are released in lockstep with the same version number.

Choosing

  • On database/sql and happy with process-wide N+1 windows? Use sqlguard.Register / OpenDB and nothing else. It sees more than any adapter — every statement, including the ones an ORM issues that its hook seam does not surface.
  • On native pgx / pgxpool? pgxguard. There is no alternative; the driver wrapper never sees those queries.
  • Need ResetN1() per request on an ORM? The matching adapter.
  • On sqlx and want everything covered? Layer sqlx over the driver wrapper (sqlx.NewDb(sqlDB, "postgres")) — it covers every sqlx method. sqlxguard wraps the common helpers and gives you ResetN1().

Using two at once — the driver wrapper and an ORM adapter on the same connection — analyzes each statement twice. Pick one per connection.

Writing your own

Any library with a before/after hook (or a single "here is the SQL" hook) can be integrated in a few dozen lines on top of middleware.NewGuard. pgxguard is the reference for split start/end hooks (Guard.Observe), gormguard for a seam that only exposes SQL after execution (Guard.Check + Guard.CheckLatency). See Guard for integration authors.