budgit/internal/repository/helpers.go
juancwu 45fcecdc04
All checks were successful
Deploy / build-and-deploy (push) Successful in 3m45s
chore: refactor
2026-03-14 16:39:07 +00:00

18 lines
387 B
Go

package repository
import "github.com/jmoiron/sqlx"
// WithTx runs fn inside a transaction. If fn returns an error, the transaction
// is rolled back; otherwise it is committed.
func WithTx(db *sqlx.DB, fn func(*sqlx.Tx) error) error {
tx, err := db.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
if err := fn(tx); err != nil {
return err
}
return tx.Commit()
}