Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dhui/satomic
Simple nested SQL transactions/savepoints in Golang
https://github.com/dhui/satomic
database golang savepoints sql transaction-manager
Last synced: 24 days ago
JSON representation
Simple nested SQL transactions/savepoints in Golang
- Host: GitHub
- URL: https://github.com/dhui/satomic
- Owner: dhui
- License: apache-2.0
- Created: 2018-12-15T07:17:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-19T12:33:00.000Z (7 months ago)
- Last Synced: 2024-10-04T17:38:58.693Z (about 1 month ago)
- Topics: database, golang, savepoints, sql, transaction-manager
- Language: Go
- Size: 190 KB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# satomic
![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/dhui/satomic/go.yml?branch=master) [![Code Coverage](https://img.shields.io/codecov/c/github/dhui/satomic.svg)](https://codecov.io/gh/dhui/satomic) [![GoDoc](https://godoc.org/github.com/dhui/satomic?status.svg)](https://godoc.org/github.com/dhui/satomic) [![Go Report Card](https://goreportcard.com/badge/github.com/dhui/satomic)](https://goreportcard.com/report/github.com/dhui/satomic) [![GitHub Release](https://img.shields.io/github/release/dhui/satomic/all.svg)](https://github.com/dhui/satomic/releases) ![Supported Go versions](https://img.shields.io/badge/Go-1.20%2C%201.21-lightgrey.svg)
satomic is a Golang package that makes managing nested SQL transactions/savepoints easier
## Overview
Create a `Querier` and use the `Atomic()` method. Any SQL statements inside the `Atomic()` method's callback function will be appropriately wrapped in a transaction or savepoint. Transaction and savepoint management will be handled for you automatically. Any error returned by the callback function (or unrecovered panic) will rollback the savepoint or transaction accordingly. A new `Querier` instance is also provided to the `Atomic()` method's callback function to allow nesting savepoints.
## Status
satomic is **not stable yet**, so the **interfaces may change in a non-backwards compatible manner**.
satomic follows [Semantic Versioning 2.0.0](https://semver.org/). While the major version number is `0`, all backwards compatible changes will be denoted by a minor version number bump. All other changes will increase the patch version number.## Example usage
```golang
package mainimport (
"context"
"database/sql"
"github.com/dhui/satomic"
"github.com/dhui/satomic/savepointers/postgres"
)// Error handling omitted for brevity. Actual code should handle errors
func main() {
ctx := context.Background()
var db *sql.DB // Use an actual db// savepointer should match the db driver used
q, _ := satomic.NewQuerier(ctx, db, postgres.Savepointer{}, sql.TxOptions{})q.Atomic(func(ctx context.Context, q satomic.Querier) error {
// In transaction
var dummy int
q.QueryRowContext(ctx, "SELECT 1;").Scan(&dummy)q.Atomic(func(ctx context.Context, q satomic.Querier) error {
// In first savepoint
return q.QueryRowContext(ctx, "SELECT 2;").Scan(&dummy)
})q.Atomic(func(ctx context.Context, q satomic.Querier) error {
// In second savepoint
q.QueryRowContext(ctx, "SELECT 3;").Scan(&dummy)q.Atomic(func(ctx context.Context, q satomic.Querier) error {
// In third savepoint
q.QueryRowContext(ctx, "SELECT 4;").Scan(&dummy)
return nil
})return nil
})return nil
})
}
```A more complete example can be found in the [GoDoc](https://godoc.org/github.com/dhui/satomic)
For usage with [sqlx](https://github.com/jmoiron/sqlx), use [github.com/dhui/satomic/satomicx](https://godoc.org/github.com/dhui/satomic/satomicx)
## What's with the name?
Go **S**QL **atomic** => satomic
---
Inspired by [sqlexp](https://github.com/golang-sql/sqlexp) and Django's [atomic](https://docs.djangoproject.com/en/2.1/topics/db/transactions/#django.db.transaction.atomic) decorator/context manager.