Tx
Overview
Tx wraps a sqlx transaction.
Part of the db package — Node.js analog: knex.
Tx is a type exported by gox. Methods on this type are documented separately.
Signature
go
type Tx struct {
SQL *sqlx.Tx
}Compare: Node.js · Standard Go · gox
js
// Typical knex pattern in Node.jsgo
db, err := sqlx.Connect("postgres", dsn)
db.GetContext(ctx, &row, query, args...)go
import "github.com/sahilkhaire/gox/db"
err := db.Transaction(ctx, func(tx *db.Tx) error {
return tx.Insert(ctx, "users", row)
})Example
go
import "github.com/sahilkhaire/gox/db"
err := db.Transaction(ctx, func(tx *db.Tx) error {
return tx.Insert(ctx, "users", row)
})Tips
Pass context.Context as the first argument so cancellation and deadlines propagate correctly.
Standard library alternative
Use the standard library directly:
go
db, err := sqlx.Connect("postgres", dsn)
db.GetContext(ctx, &row, query, args...)