https://github.com/sawka/txwrap
TxWrap implements a transaction wrapper around a *sqlx.Tx transaction that simplifies error handling, commits, and rollbacks.
https://github.com/sawka/txwrap
database golang
Last synced: 2 months ago
JSON representation
TxWrap implements a transaction wrapper around a *sqlx.Tx transaction that simplifies error handling, commits, and rollbacks.
- Host: GitHub
- URL: https://github.com/sawka/txwrap
- Owner: sawka
- License: mit
- Created: 2023-02-14T20:55:55.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-13T18:35:06.000Z (about 2 years ago)
- Last Synced: 2025-01-23T09:55:32.889Z (over 1 year ago)
- Topics: database, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/sawka/txwrap
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TxWrap
TxWrap implements a transaction wrapper around a *sqlx.Tx transaction that simplifies error handling
and is guaranteed to either call Commit or Rollback on all transactions it starts.
TxWrap will automatically handle all database errors when using the TxWrap interface. After an error
occurs, all future database calls using the same TxWrap will be short-circuited and fail immediately
and the transaction will be rolled back.
Usage:
```
txErr := WithTx(ctx, db, func(tx *txwrap.TxWrap) error {
query := `SELECT sessionid FROM sessions WHERE sessionid = ?`
if !tx.Exists(query, sessionId) {
query = `INSERT INTO session (sessionid, counter) VALUES (?, 0)`
tx.Exec(query, sessionId)
}
query = `UPDATE session SET counter = counter + 1 WHERE sessionid = ?`
tx.Exec(query, sessionId)
return nil
})
```
Note that no error handling is necessary. If any of the database calls (Exists, Exec) fail
the transaction will be automatically rolled back. If the function completes with no errors
(and the return value is nil) the transaction will be committed. By returning a non-nil
error, you can also force a rollback at any time.