Skip to content
/ txn Public

An interface that aims to provide data consistency in modern architectures without reducing business logic to repositories.

License

Notifications You must be signed in to change notification settings

9ssi7/txn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

txn: Generic Distributed Transaction Management for Go

GoDoc Project status Go Report Card

The txn package provides a robust and flexible framework for managing distributed transactions across multiple data sources in your Go applications. By harnessing the power of Go generics, txn enables a clean, database-agnostic approach to transaction handling, ensuring data consistency and atomicity even in complex, distributed environments.

Before of all, Check the Real World Example

Key Features

  • Distributed Transactions: Coordinate transactions across multiple data sources seamlessly.
  • Clean Architecture: Maintain a clear separation of concerns, keeping your business logic decoupled from data access details.
  • Atomicity: Ensure that all operations within a transaction either succeed or fail together, maintaining data integrity.
  • Flexibility: Easily extend the framework by creating custom adapters for your specific data sources.

Note: Database independency not possible with this package. You need to use the same database for all adapters. For example, if you are using GORM, you need to use GORM for all adapters. If you are using MongoDB, you need to use MongoDB for all adapters. If GORM throws an error but MongoDB doesn't, you need to handle it yourself. This package doesn't handle that. It only provides a way to manage transactions across multiple data sources.

Installation

go get github.com/9ssi7/txn

go get github.com/9ssi7/txn/txngorm // For GORM Adapter
go get github.com/9ssi7/txn/txnmongo // For MongoDB Adapter
go get github.com/9ssi7/txn/txnsql // For Native SQL Adapter

Usage

  1. Create a Tx Instance:
tx := txn.New()
  1. Register Adapters:
tx.Register(txngorm.New(gormDB), txnmongo.New(mongoClient), txnsql.New(sqlDB))

// Register more adapters as needed...
  1. Manage Transactions:
func transaction() (err error) {
    defer func() {
        if err != nil {
            tx.Rollback(context.Background())
        }
    }()
    if err := tx.Begin(context.Background()); err != nil {
        return err
    }
    // Perform operations on each data source using their respective adapters
    // ...

    if err := tx.Commit(context.Background()); err != nil {
    return err
    }
    return nil
}

Adapters

The txn package supports multiple database adapters:

Contributing

Contributions are welcome! Please feel free to submit issues, bug reports, or pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

An interface that aims to provide data consistency in modern architectures without reducing business logic to repositories.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published