sqlxtx

package module
v0.0.2-0...-986e9f8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2021 License: MIT Imports: 3 Imported by: 0

README

Sqlxtx CircleCI Coverage Status

Changes behavior between sqlx.DB and sqlx.Tx in runtime.

Installation

go get -u github.com/andresmijares/sqlxtx

Usage

// db.go
// Will manage each operation independently 
func init() {
    datasourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
		username,
		password,
		host,
		schema)
	var err error

	Client, err = sqlx.Open("mysql", datasourceName)
	if err != nil {
		panic(err)
	}

	WithTx = &sqlxtx.EnableSqlxTx{
		Client: Client,
		Config: sqlxtx.Config{
			Verbose: true,
		},
	}
}
// domain.go
type User struct {
	Client *sqlx.DB
}

func (u *User) Create(username, password string) error {
    _, err := u.Client.NamedExec("INSERT INTO users (first_name, last_name) VALUES (:first_name, :last_name);",
		map[string]interface{}{
			"first_name": "Jhon",
			"last_name":  "Doe",
		})
    if err != nil {
		return err
    }
    return nil
}

type Events struct {
	Client *sqlx.DB
}

func (u *Events) Create(name string) error {
    _, err := e.Client.NamedExec(`INSERT INTO events(name) VALUE (:name);`,
		map[string]interface{}{
			"name": name,
		})
    if err != nil {
		return err
    }
    return nil
}

// service.go
UserDao := &User{
	Client: Client,
}
EventsDao := &Events{
	Client: Client,
}

func CreateUser () {
    if err := UserDao.Create(); err != nil {
		return err
    }

    if err := EventsDao.Create("userCreated"); err != nil {
		return err
    }

    return nil
}

// Will run a transaction, not need to modify the underline implementation
func CreateUserWithTx () {
    if err := db.WithTx.Exec(func() error {
	if err := UserDao.Create(); err != nil {
	    return err
	}

	if err := EventsDao.Create("userCreated"); err != nil {
	    return err
	}

		return nil
    }); err != nil {
		return err
    }
    return nil
}

Test

Sample how to test this can be found in the sample folder.

Limitations

I've mocked only the most commons methods I use, for a more detailed list, please check sqltxmocks

Nested transactions are not supported, yet.

Motivation

Transactions should be implementation details, it should not force developers to re-write code to support between Tx and DB. I couldn't find a solid way to decorate operations in my services, so I created this one.

I found a lot of motivation in this articles.

I only added the methods I use, please, feel free to submit PR's to support more methods.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Verbose bool
}

Config used as configuration object for EnableSqlxTx

type EnableSqlxTx

type EnableSqlxTx struct {
	Client *sqlx.DB
	Config Config
}

EnableSqlxTx init SqlxTx

func (*EnableSqlxTx) Exec

func (repo *EnableSqlxTx) Exec(txFn func() error) error

Exec receives a callback meant to swap sqlx.DB by sqlx.Tx in runtime it will apply to operation into the callback

type SQLxTx

type SQLxTx struct {
	DB *sqlx.Tx
}

SQLxTx is wrapper around sqlx.Tx, to change behavior in runtime between sqlx.DB and sqlx.Tx

func (*SQLxTx) Commit

func (sdt *SQLxTx) Commit() error

Commit commits the transaction.

func (*SQLxTx) Exec

func (sdt *SQLxTx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*SQLxTx) Get

func (sdt *SQLxTx) Get(dest interface{}, query string, args ...interface{}) error

Get within a transaction. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*SQLxTx) NamedExec

func (sdt *SQLxTx) NamedExec(query string, args interface{}) (sql.Result, error)

NamedExec within a transaction. Any named placeholder parameters are replaced with fields from arg.

func (*SQLxTx) Rollback

func (sdt *SQLxTx) Rollback() error

Rollback aborts the transaction.

func (*SQLxTx) Select

func (sdt *SQLxTx) Select(dest interface{}, query string, args ...interface{}) error

Select within a transaction.

func (*SQLxTx) TxEnd

func (sdt *SQLxTx) TxEnd(txFn func() error, config Config) error

TxEnd Finish a transaction

type SqlxTxInterface

type SqlxTxInterface interface {
	Exec(txFn func() error) error
}

SqlxTxInterface exposes a workable interface for SqlxTx

type SqlxWrapperInterface

type SqlxWrapperInterface interface {
	Select(dest interface{}, query string, args ...interface{}) error
	Get(dest interface{}, query string, args ...interface{}) error
	NamedExec(query string, args interface{}) (sql.Result, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
	Commit() error
	Transactions
}

SqlxWrapperInterface Wraps sqlx interface

type Transactions

type Transactions interface {
	Rollback() error
	Commit() error
	TxEnd(txFn func() error, config Config) error
}

Transactions wraps sqlx Transactions functionality

Directories

Path Synopsis
db

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL