Skip to content
piprimPublic

About

A Postgres-only CRUD ORM-like that runs only with pgxpool

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Boa

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.

Usage

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.

Notes

  • Queries use bun's ? placeholders. boa translates them to $1..$n and 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 and q.Args() the values.
  • A raw query that contains no ? is passed to pgx unchanged with its args, so pgx-style SQL with $1 works too. Do not mix ? and $n in 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 $n would point at the wrong parameters, so boa rejects that case with an error.
  • A parameter with nothing to type it, such as SELECT ? or ColumnExpr("?", v), needs a cast in the SQL (?::int8); Postgres cannot infer its type from a bare placeholder.
  • A field whose type has an IsUnset() bool method, such as presence.Of[T], is written as DEFAULT by 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 and pgdialect.Array(v) are sent as Postgres arrays; pgdialect.Range values 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.ErrTooManyParams before anything is sent; chunk the rows or use pgx.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; pass boa.WithQueryExecMode(pgx.QueryExecModeCacheDescribe) if the application alters tables while running.
  • Errors are pgx errors: use errors.Is(err, pgx.ErrNoRows) and errors.As(err, &pgErr) with *pgconn.PgError.
  • SelectQuery.Rows returns pgx.Rows, so pgx.CollectRows works for projections that do not need the model scanner.
  • Model struct tags use the boa:"..." syntax.
  • PostgreSQL's infinity and -infinity dates are read as boa.PosInfinity and boa.NegInfinity, and those two values are written back as infinity and -infinity. Compare with Equal. A time inside an array is not translated.

Tests

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.

License

BSD-2-Clause, inherited from bun. See LICENSE.

About

A Postgres-only CRUD ORM-like that runs only with pgxpool

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages