entguard

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 entguard integrates sqlguard with ent (entgo.io/ent).

ent runs on database/sql, so the simplest coverage is already available by pointing entsql at a *sql.DB obtained from sqlguard.Register / OpenDB. This package is the dedicated alternative: it decorates ent's own dialect.Driver seam, so it works regardless of how the underlying *sql.DB was opened (including ent's dialect.DebugDriver chain) and mirrors ent's built-in dialect.Debug wrapper.

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 every other sqlguard surface. There is no parallel option surface — configure with the standard middleware options:

drv, _ := entsql.Open(dialect.Postgres, dsn)
guarded := entguard.Wrap(drv,
    middleware.WithSlowQueryThreshold(500*time.Millisecond),
    middleware.WithN1Detection(10, time.Second),
)
client := ent.NewClient(ent.Driver(guarded))

Every Exec/Query — on the driver and on transactions it opens — flows through middleware.Guard.Observe: static rules run on every call, latency is recorded only on success.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver struct {
	dialect.Driver
	// contains filtered or unexported fields
}

Driver decorates an ent dialect.Driver, routing every statement through the shared sqlguard analysis core.

func Wrap

func Wrap(d dialect.Driver, opts ...middleware.Option) *Driver

Wrap decorates an ent dialect.Driver. It accepts the standard sqlguard middleware options (WithAnalyzer, WithReporter, WithSlowQueryThreshold, WithParser, WithN1Detection, …) — the same option set every other sqlguard surface uses, so there is no parallel configuration surface to drift.

func (*Driver) BeginTx

func (d *Driver) BeginTx(ctx context.Context, opts *sql.TxOptions) (dialect.Tx, error)

BeginTx forwards to the wrapped driver's BeginTx when it implements one (entsql.Driver does — this is how ent honours read-only / isolation options), and wraps the resulting transaction. It degrades to Tx when the base driver has no BeginTx, matching ent's own fallback.

func (*Driver) Exec

func (d *Driver) Exec(ctx context.Context, query string, args, v any) error

Exec implements dialect.ExecQuerier.

func (*Driver) Query

func (d *Driver) Query(ctx context.Context, query string, args, v any) error

Query implements dialect.ExecQuerier.

func (*Driver) ResetN1

func (d *Driver) 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 Wrap.

func (*Driver) Tx

func (d *Driver) Tx(ctx context.Context) (dialect.Tx, error)

Tx wraps the transaction so statements executed inside it are analyzed too.

Jump to

Keyboard shortcuts

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