Documentation
¶
Overview ¶
Package bunx is the optional bun adapter for the kit's transaction model.
The core dbx/pg package carries a native pgx.Tx in the context; a project that prefers bun for query building imports this package instead. It provides a dbx.Transactor whose transaction is a bun.Tx carried in the context, plus the resolvers repositories use to join it. Nothing here is linked into a binary that does not import it, so the core kit stays bun-free.
Wiring (in main, replacing dbx/pg's NewTransactor):
bunDB := bunx.Open(pool) // reuses the kit's pgxpool tx := bunx.NewTransactor(bunDB)
A bun repository holds the root *bun.DB and resolves the query surface from the context at the top of each method, so it joins the transaction opened by WithinTransaction automatically:
func (r ordersRepo) Insert(ctx context.Context, o *Order) error {
_, err := bunx.From(ctx, r.db).NewInsert().Model(o).Exec(ctx)
return err
}
Because a bun.Tx embeds a *sql.Tx, raw SQL and sqlc (database/sql mode) join the same transaction through Conn:
_, err := bunx.Conn(ctx, r.db).ExecContext(ctx, `UPDATE ...`, args...)
The one constraint: within a single project the pgx-native executor (dbx/pg's DB) and bun cannot share one transaction — they check out connections through different abstractions. A bun project routes its raw SQL through Conn, not through dbx/pg's DB.
Index ¶
- func Conn(ctx context.Context, db *bun.DB) bun.IConn
- func ContextWithTx(ctx context.Context, tx bun.Tx) context.Context
- func From(ctx context.Context, db bun.IDB) bun.IDB
- func NewTransactor(db *bun.DB) dbx.Transactor
- func Open(pool *pgxpool.Pool) *bun.DB
- func TxFromContext(ctx context.Context) (bun.Tx, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Conn ¶
Conn returns the raw database/sql query surface (bun.IConn) to use for raw SQL or sqlc: the transaction carried by ctx when one is present, otherwise the root db. This is the path by which non-bun code joins a bun transaction.
It deliberately returns the *underlying* *sql.Tx / *sql.DB (bun.Tx embeds a *sql.Tx, bun.DB a *sql.DB), not the bun wrappers. The wrappers reformat every query through bun's placeholder syntax (?), which would break native $N placeholders; the raw handles pass the query straight to the pgx driver, so sqlc-generated postgres SQL runs unchanged. The trade-off is that queries run this way bypass bun's query hooks (logging/otel) — code wanting those uses the bun query builder via From instead.
func ContextWithTx ¶
ContextWithTx returns a context carrying the transaction. Exposed for custom executors; application code normally never calls it.
func From ¶
From returns the bun.IDB to use for a query: the transaction carried by ctx when WithinTransaction opened one, otherwise the given root db. Both bun.Tx and *bun.DB satisfy bun.IDB, so callers use the result uniformly.
func NewTransactor ¶
func NewTransactor(db *bun.DB) dbx.Transactor
NewTransactor returns a dbx.Transactor backed by bun. It satisfies the same interface as dbx/pg's NewTransactor, so the service layer and scaffolded code are identical regardless of which one a project wires up.
Types ¶
This section is empty.