Giter Site home page Giter Site logo

go-uow's Introduction

Go Unit of Work

This package is an implementation of the unit of work pattern in Golang. Unit of work is a design pattern that is widely used to help manage transactions and maintain consistency in an application. When combined with a clean architecture, this can provide a solid foundation for building scalable and maintainable applications.

Installation

The following command is used for install this package into your golang project

go get github.com/Muruyung/Go-UoW@latest

How to Use

Example using NewSession

This implementation is used if you want to customize the placement of new sessions, commits, and rollbacks

package main

import (
    "context"
    "database/sql"

    "github.com/Muruyung/Go-UoW/gouow" // import gouow package
)

var (
    db *gorm.DB // Example db engine
)

func main() {
    var (
        db  = db.GormInit()
        uow = gouow.Init(db) // Initialize unit of work
        ctx = context.TODO()
        err error
    )

    err = uow.NewSession(&ctx) // Create new session for start transaction
    if err != nil {
        return
    }

    err = RepositoryFunction(ctx) // Call first function
    if err != nil {
        _ = gouow.Rollback(err) // It will rollback RepositoryFunction if there is an error
        return
    }

    err = AnotherFunction(ctx) // Call another function
    if err != nil {
        _ = gouow.Rollback(err) // It will rollback RepositoryFunction and AnotherFunction if there is an error
        return
    }

    err = uow.Commit() // Commit all transaction process
    if err != nil {
        return
    }
}

// Example function for repository
func RepositoryFunction(ctx context.Context) error {
    var (
        sqlDB = db
        tx    = ctx.Value(gouow.TX_KEY)
        err   error
    )

    if tx != nil {
        if dbTx := tx.(*gouow.TX); dbTx.UseTx {
            sqlDB = dbTx.GormDB()
        }
    }

    // Implement your repository logic here ...

    return err
}

// Example function for another logic outside of repository logic
func AnotherFunction(ctx context.Context) error {
    var err error
    // Implement your another logic here ...
    return err
}

Example using BeginTx

This implementation is used if you want to use a simple method

package main

import (
    "context"
    "database/sql"

    "github.com/Muruyung/Go-UoW/gouow" // import gouow package
)

var (
    db *gorm.DB // Example db engine
)

func main() {
    var (
        db  = db.GormInit()
        uow = gouow.Init(db) // Initialize unit of work
        ctx = context.TODO()
    )

    err := uow.BeginTx(ctx, func(ctxTx context.Context) error {
        err := RepositoryFunction(ctxTx) // Call first function
        if err != nil {
            return err // It will return error and rollback RepositoryFunction
        }

        err = AnotherFunction(ctxTx) // Call another function
        if err != nil {
            return err // It will return error and rollback RepositoryFunction and AnotherFunction
        }

        return nil // It will return nil and commit all process
    })
    if err != nil {
        return
    }
}

// Example function for repository
func RepositoryFunction(ctx context.Context) error {
    var (
        sqlDB = db
        tx    = ctx.Value(gouow.TX_KEY)
        err   error
    )

    if tx != nil {
        if dbTx := tx.(*gouow.TX); dbTx.UseTx {
            sqlDB = dbTx.GormDB()
        }
    }

    // Implement your repository logic here ...

    return err
}

// Example function for another logic outside of repository logic
func AnotherFunction(ctx context.Context) error {
    var err error
    // Implement your another logic here ...
    return err
}

go-uow's People

Contributors

muruyung avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.