A Postgres-only CRUD ORM-like derived from bun that runs on
pgxpool.Pool and pgx.Tx instead of database/sql.
It keeps bun's query builders, relations and hooks for Select, Insert, Update, Delete, Raw and Values queries, and drops migrations, fixtures, DDL builders and non-Postgres dialects.
pool, _ := pgxpool.New(ctx, dsn)
uow := uow.New(pool) // your unit of work
db := boa.New(pool, boa.WithExecutorResolver(uow.Executor))
var users []User
err := db.NewSelect().Model(&users).Where("active = ?", true).Scan(ctx)
err = uow.WithTransaction(ctx, func(ctx context.Context) error {
_, err := db.NewInsert().Model(&User{Name: "ann"}).Exec(ctx)
return err
})The resolver picks the executor for every query from the context: the pgx.Tx your unit of
work stored there, or the pool. boa never begins, commits or rolls back a transaction.
Pass boa.WithTxRequiredForWrites() to make Insert, Update, Delete, DB.Exec and
non-SELECT Raw queries fail with boa.ErrTxRequired when no transaction is attached to
the context. Reads still run on the pool.
- Queries use bun's
?placeholders. boa translates them to$1..$nand passes the values to pgx, so statements are prepared and cached per connection and parameters travel in binary.q.String()shows the SQL that is sent andq.Args()the values. - A raw query that contains no
?is passed to pgx unchanged with its args, so pgx-style SQL with$1works too. Do not mix?and$nin one query, and use such a raw query only on its own or as the first bound part of a larger statement: nested after other bound values its$nwould point at the wrong parameters, so boa rejects that case with an error. - A parameter with nothing to type it, such as
SELECT ?orColumnExpr("?", v), needs a cast in the SQL (?::int8); Postgres cannot infer its type from a bare placeholder. - A field whose type has an
IsUnset() boolmethod, such aspresence.Of[T], is written asDEFAULTby Insert when unset and left out of SET by a struct Update. Null and values are written as usual, and a NULL result scans to the type's null, never to unset. A bulk Update (Model(&slice).Bulk()) writes every column, so it fails with an error when a row has an unset field instead of writing NULL. - Untagged structs, maps and slices are sent as JSON;
boa:",array"fields andpgdialect.Array(v)are sent as Postgres arrays;pgdialect.Rangevalues are sent as text and typed by Postgres from the column or operator. Where nothing fixes the type, cast in the SQL:?::daterange. - Postgres accepts at most 65535 parameters per statement. A larger statement fails with
boa.ErrTooManyParamsbefore anything is sent; chunk the rows or usepgx.CopyFrom. - Result columns arrive in pgx's preferred format, binary for most scalar types. Arrays,
ranges and multiranges are requested in text because boa parses them itself. If your
pool registers extra types with a binary codec that boa must scan as text, list their
OIDs with
boa.WithTextResultTypes(oids...). - pgx's default execution mode caches prepared statements per connection. After a schema
change a cached statement can fail with
cached plan must change result type; passboa.WithQueryExecMode(pgx.QueryExecModeCacheDescribe)if the application alters tables while running. - Errors are pgx errors: use
errors.Is(err, pgx.ErrNoRows)anderrors.As(err, &pgErr)with*pgconn.PgError. SelectQuery.Rowsreturnspgx.Rows, sopgx.CollectRowsworks for projections that do not need the model scanner.- Model struct tags use the
boa:"..."syntax. - PostgreSQL's
infinityand-infinitydates are read asboa.PosInfinityandboa.NegInfinity, and those two values are written back asinfinityand-infinity. Compare withEqual. A time inside an array is not translated.
make test starts the test database, runs go vet and every test, then removes the
database. The same by hand:
docker compose up -d --wait
BOA_TEST_DSN='postgres://postgres:postgres@localhost:5442/boa?sslmode=disable' go test -count=1 ./...Without BOA_TEST_DSN the integration tests are skipped.
BSD-2-Clause, inherited from bun. See LICENSE.