ysomad / pgxatomic

Clean implementation of transaction manager using pgx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pgxatomic

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.

schema

Example Usage

  1. You can use pgxatomic.Pool within repository implementation. It's simple wrapper around pgxpool.Pool which is wrapping Query, QueryRow and Exec methods with pgxatomic 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.

  1. 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!

Credits

About

Clean implementation of transaction manager using pgx

License:MIT License


Languages

Language:Go 100.0%