pgxatomic is a library of tools that allow you to implement transfer of clean control to transactions to a higher level by adding transaction in a context.Context
using pgx driver.
- You can use
pgxatomic.Pool
within repository implementation. It's simple wrapper aroundpgxpool.Pool
which is wrappingQuery
,QueryRow
andExec
methods withpgxatomic
query functions.
type orderRepo struct {
pool *pgxatomic.Pool // pgxpool.Pool wrapper
}
type order struct {
ID uuid.UUID
Cost int
}
func (r *orderRepo) Insert(ctx context.Context, cost int) order {
rows, _ := r.pool.Query(ctx, "insert into order(cost) values ($1) RETURNING id, cost", cost)
o, _ := pgx.CollectOneRow(rows, pgx.RowToStructByPos[order])
return o
}
Or you can use Query
, QueryRow
, Exec
functions directly from the library.
- Run wrapped usecase method calls within txFunc using
pgxatomic.runner.Run
function
conf, _ := pgxpool.ParseConfig("postgres://user:pass@localhost:5432/postgres")
pool, _ := pgxpool.NewWithConfig(context.Background(), conf)
r, _ := pgxatomic.NewRunner(pool, pgx.TxOptions{})
_ = r.Run(context.Background(), func(txCtx context.Context) error {
_ = orderService.Create(txCtx)
_ = balanceService.Withdraw(txCtx)
return nil
})
Error handling is omitted on purpose, handle all errors!