Documentation
¶
Overview ¶
Package dbx is the database-agnostic contract of the kit: Config describes a PostgreSQL connection and Transactor is the interface services depend on to span multiple repositories in one transaction. It has no database driver dependency of its own.
Two implementations exist, selected by which one a project imports:
- dbx/pg — the pgx-native stack (pool construction, a DBTX query executor, and a Transactor backed by pgx.Tx).
- dbx/bunx — an optional adapter for projects that prefer the bun query builder, backed by bun.Tx.
Only one is ever linked into a given binary; a service depends on dbx.Transactor and dbx.Config, never on the concrete implementation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrBeginTx = errors.New("dbx: begin transaction") ErrCommitTx = errors.New("dbx: commit transaction") )
ErrBeginTx and ErrCommitTx identify which transaction lifecycle step failed, regardless of which Transactor implementation (dbx/pg or dbx/bunx) is in use. Both implementations wrap the driver error with one of these via errors.Is-compatible %w; a caller that needs to distinguish "the database is unreachable" from "a constraint failed at commit" matches on these instead of the underlying driver error type.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Host string `env:"HOST" envDefault:"localhost"`
Port int `env:"PORT" envDefault:"5432"`
User string `env:"USER,required"`
Password string `env:"PASSWORD,required"`
Database string `env:"NAME,required"`
SSLMode string `env:"SSLMODE" envDefault:"disable"`
MaxConns int32 `env:"MAX_CONNS" envDefault:"10"`
}
Config describes a PostgreSQL connection. Compose it under a prefix:
type Config struct {
DB dbx.Config `envPrefix:"DB_"`
}
which maps to DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME, DB_SSLMODE, DB_MAX_CONNS. A second, independent connection composes under its own prefix the same way (the kit's `add db` generator does this for named connections):
type Config struct {
DB dbx.Config `envPrefix:"DB_"`
DBAnalytics dbx.Config `envPrefix:"DB_ANALYTICS_"`
}
type Transactor ¶
type Transactor interface {
WithinTransaction(ctx context.Context, fn func(ctx context.Context) error) error
}
Transactor runs a function within a database transaction. The transaction is carried by the context, so every DB call made through the kit's executors — dbx/pg's DB or dbx/bunx's resolvers — inside fn joins it automatically.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bunx is the optional bun adapter for the kit's transaction model.
|
Package bunx is the optional bun adapter for the kit's transaction model. |
|
Package pg is the pgx-native database stack: pool construction, a transaction-aware query executor (DB), and a Transactor (satisfying dbx.Transactor) that lets a service span multiple repositories with one transaction.
|
Package pg is the pgx-native database stack: pool construction, a transaction-aware query executor (DB), and a Transactor (satisfying dbx.Transactor) that lets a service span multiple repositories with one transaction. |
|
Package seed is the database-agnostic contract of the kit's seeder subsystem: a Registry of named Seeders, each optionally guarded, run after migrations to load idempotent baseline data (an admin user, roles, lookup tables).
|
Package seed is the database-agnostic contract of the kit's seeder subsystem: a Registry of named Seeders, each optionally guarded, run after migrations to load idempotent baseline data (an admin user, roles, lookup tables). |
|
bunx
Package bunx is the seed subsystem's optional bun adapter: FirstOrCreate, the idempotent primitive seeders are built around.
|
Package bunx is the seed subsystem's optional bun adapter: FirstOrCreate, the idempotent primitive seeders are built around. |