Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

andrdru/sqltx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang SQL transaction

Helper for sql transactions

Install

go get -u github.com/andrdru/sqltx

Usage

see examples

// add helper to repo

User interface {
  sqltx.Tx

  TX(action func(txRepo User) error) error
}

// TX simplify DoTransaction call for User interface
func (m *user) TX(action func(txRepo User) error) error {
  return m.DoTransaction(
    func(tx sqltx.QueryExecutor) sqltx.Tx { return NewUser(tx) },
    func(txRepo sqltx.Tx) error { return action(txRepo.(User)) },
  )
}
// call

err = userRepo.TX(func(txRepo repo.User) (err error) {
  err = txRepo.CreateUser(ctx, 1, "Vasya")
  if err != nil {
    return err
  }
  
  return nil
}

if err != nil {
  log.Fatalf("transaction rollback: %s", err)
}

Packages

No packages published

Languages