Documentation
¶
Overview ¶
Package dbx provides SQL-first data access helpers for Go, built on top of pgx. It eliminates boilerplate while keeping SQL front and center.
Key features:
- QueryMaps: Get results as []map[string]interface{}
- QueryStructs: Map results into structs using db:"table.column" tags
- InsertStruct: Insert structs into tables automatically
- QueryJSON: Get results as JSON bytes
Example:
rows, err := dbx.QueryMaps(ctx, db, "SELECT * FROM users WHERE active = $1", true) var users []User err = dbx.QueryStructs(ctx, db, "SELECT * FROM users", &users) err = dbx.InsertStruct(ctx, db, "users", user)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InsertStruct ¶
InsertStruct inserts a struct into the specified table. It uses db:"column" tags to map struct fields to table columns. Fields without db tags or with db:"-" are ignored.
func QueryJSON ¶
QueryJSON executes a query and returns results as JSON bytes. This is useful for APIs or when you need JSON output directly.
func QueryStructs ¶
QueryStructs executes a query and maps results into the provided struct slice. It uses db:"table.column" tags to map columns to struct fields. The dest parameter must be a pointer to a slice of structs.
Types ¶
type DB ¶
type DB interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}
DB is an interface that provides the methods needed for database operations