hyperbun

package module
v0.0.0-...-657f1e1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 12 Imported by: 0

README

Hyperbun

Hyperbun is a utility library built on top of Bun to reduce boilerplate code.

Queries like this in bun:

func Foo() {
  // ...
	
  var user User
  if err := db.NewSelect().Model(&user).Where("id = ?", id).Scan(c.Request.Context()); err != nil {
    if errors.Is(err, sql.ErrNoRows) {
      // ...
    }
	// ...
  }
		
  db.NewDelete().Model(&user).Where("id = ?", id).Exec(c.Request.Context())
}

can be written like this with Hyperbun:

func Foo() {
  hdb := hyperbun.NewContext(c.Request.Context(), db)

  user, err := hyperbun.ByID(hdb, "users", id)
  if err != nil {
	// ...	
  }	
  if user == nil {
    // ...	
  }
	
  hyperbun.DeleteByID(hdb, "users", id)
}

It also makes working with transactions easier and tx and hdb can be used interchangeably. This promotes code reuse.

func Foo(db hyperbun.DB) {
  hyperbun.DeleteByID(db, "users", id)
}

func main() {
  hdb := hyperbun.NewContext(c.Request.Context(), db)

  hyperbun.RunInTx(hdb, func(tx hyperbun.TxContext) error {
    Foo(tx)
    return nil
  })

  Foo(hdb)
}

License

  • Distributed under MIT License, please see license file within the code for more details.

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByID

func ByID[T any, ID string | int](m DB, id ID) (*T, error)

func BySQL

func BySQL[T any](m DB, query string, args ...interface{}) (*T, error)

func CountQuery

func CountQuery(m DB, table string, query string, args ...interface{}) (int, error)

func DeleteByID

func DeleteByID[ID string | int](m DB, table string, id ID) error

func DeleteBySQL

func DeleteBySQL(m DB, table string, query string, args ...interface{}) error

func Exists

func Exists[ID string | int](m DB, table string, id ID) (bool, error)

func ExistsBySQL

func ExistsBySQL(m DB, table string, query string, args ...interface{}) (bool, error)

func Insert

func Insert[T any](m DB, row *T) error

func InsertMany

func InsertMany[T any](m DB, rows []T) error

func Many

func Many[T any](m DB, query string, args ...interface{}) ([]T, error)

func RunInLockedTx

func RunInLockedTx(m DB, id string, fn func(tx TxContext) error) error

func RunInTx

func RunInTx(m DB, fn func(tx TxContext) error) error

func StructByID

func StructByID[T any, ID string | int](m DB, table string, id ID) (*T, error)

func StructBySQL

func StructBySQL[T any](m DB, table string, query string, args ...interface{}) (*T, error)

func TypeByID

func TypeByID[T any, ID string | int](m DB, table string, column string, id ID) (*T, error)

func TypeBySQL

func TypeBySQL[T any](m DB, table string, column string, query string, args ...interface{}) (*T, error)

func Update

func Update[T any](m DB, row *T, pk ...string) error

func UpdateSQLByID

func UpdateSQLByID[ID string | int](m DB, table string, id ID, query string, args ...interface{}) error

func Upsert

func Upsert[T any](m DB, rows T, conflictColumns string) error

To upsert and check multiple constraints, see https://stackoverflow.com/questions/35888012/use-multiple-conflict-target-in-on-conflict-clause

func UpsertIgnore

func UpsertIgnore[T any](m DB, rows T) error

Types

type Context

type Context struct {
	Bun *bun.DB
	// contains filtered or unexported fields
}

func NewContext

func NewContext(ctx context.Context, db *bun.DB) *Context

func (Context) Context

func (m Context) Context() context.Context

func (Context) NewDelete

func (m Context) NewDelete() *bun.DeleteQuery

func (Context) NewInsert

func (m Context) NewInsert() *bun.InsertQuery

func (Context) NewMerge

func (m Context) NewMerge() *bun.MergeQuery

func (Context) NewRaw

func (m Context) NewRaw(query string, args ...interface{}) *bun.RawQuery

func (Context) NewSelect

func (m Context) NewSelect() *bun.SelectQuery

func (Context) NewUpdate

func (m Context) NewUpdate() *bun.UpdateQuery

func (Context) NewValues

func (m Context) NewValues(model interface{}) *bun.ValuesQuery

func (Context) RunInTx

func (m Context) RunInTx(fn func(tx TxContext) error) error

type DB

type DB interface {
	Context() context.Context
	NewSelect() *bun.SelectQuery
	NewInsert() *bun.InsertQuery
	NewUpdate() *bun.UpdateQuery
	NewDelete() *bun.DeleteQuery
	NewMerge() *bun.MergeQuery
	NewRaw(string, ...interface{}) *bun.RawQuery
	NewValues(model interface{}) *bun.ValuesQuery
	RunInTx(fn func(tx TxContext) error) error
}

type TxContext

type TxContext struct {
	Bun bun.Tx
	// contains filtered or unexported fields
}

func NewTxContext

func NewTxContext(ctx context.Context, tx bun.Tx) TxContext

func (TxContext) Context

func (m TxContext) Context() context.Context

func (TxContext) NewDelete

func (m TxContext) NewDelete() *bun.DeleteQuery

func (TxContext) NewInsert

func (m TxContext) NewInsert() *bun.InsertQuery

func (TxContext) NewMerge

func (m TxContext) NewMerge() *bun.MergeQuery

func (TxContext) NewRaw

func (m TxContext) NewRaw(query string, args ...interface{}) *bun.RawQuery

func (TxContext) NewSelect

func (m TxContext) NewSelect() *bun.SelectQuery

func (TxContext) NewUpdate

func (m TxContext) NewUpdate() *bun.UpdateQuery

func (TxContext) NewValues

func (m TxContext) NewValues(model interface{}) *bun.ValuesQuery

func (TxContext) RunInTx

func (m TxContext) RunInTx(fn func(tx TxContext) error) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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