Skip to content

Go jackc/pgx helper to write proper transactions

License

Notifications You must be signed in to change notification settings

cristalhq/pgxutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgxutil

build-img pkg-img reportcard-img coverage-img

Go jackc/pgx helper to write proper transactions.

Features

  • Simple API.

Install

Go version 1.17+

go get github.com/cristalhq/pgxutil

Example

// create jackc/pgx pool
var pool *pgxpool.Pool

db, err := pgxutil.New(pool)
if err != nil {
	panic(err)
}

ctx := context.Background()

// to make transaction with a given isolation level
level := pgx.Serializable
errTx := db.InWriteTx(ctx, level, func(tx pgx.Tx) error {
	// TODO: good query with tx
	return nil
})
if errTx != nil {
	panic(errTx)
}

// to make read-only transaction with a read committed isolation level
errRead := db.InReadTx(ctx, func(tx pgx.Tx) error {
	// TODO: good read-only query with tx
	return nil
})
if errRead != nil {
	panic(errRead)
}	

Also see examples: examples_test.go.

Documentation

See these docs.

License

MIT License.