bao

package
v0.0.0-...-d0a274c Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 9 Imported by: 0

README

bao

bao provides a few additional features for Bun:

  • Helper functions with type parameters for finding, saving, and deleting models
  • Transaction-aware before and after hooks for create, update, and delete operations
  • "Nested transaction" handling

Basic Usage

type user struct {
    ID        string `bun:",pk"`
    NameFirst string
    NameLast  string
}

newUser := &user{
    ID:        uuid.New().String(),
    NameFirst: "John",
    NameLast:  "Smith",
}

// db is a *bun.DB
err := bao.Create(context.Background(), db, newUser, nil, nil)
if err != nil {
    log.Fatal(err)
}

Hooks

type user struct {
    ID        string `bun:",pk"`
    NameFirst string
    NameLast  string
}

myBeforeSaveHook := func(ctx context.Context, db bun.IDB, model *user) error {
    fmt.Println("before save")
    return nil
}

myAfterSaveHook := func(ctx context.Context, model *user) {
    fmt.Println("after save")
}

newUser := &user{
    ID:        uuid.New().String(),
    NameFirst: "John",
    NameLast:  "Smith",
}

// db is a *bun.DB
err := bao.Create(
    context.Background(),
    db,
    newUser,
    []hook.Before[user]{myBeforeSave},
    []hook.After[user]{myAfterSave},
)
if err != nil {
    log.Fatal(err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrModelNotStruct = errors.New("model must be a pointer to a struct")
View Source
var ErrModelNotStructOrSlice = errors.New("model must be a struct or a slice")
View Source
var ErrOnePrimaryKey = errors.New("table must have exactly one primary key")
View Source
var ErrUpdateNotExists = errors.New("model to be updated does not exist")

Functions

func Create

func Create[ModelT any](ctx context.Context, db bun.IDB, model *ModelT, befores []hook.Before[ModelT], afters []hook.After[ModelT]) error

func Delete

func Delete[ModelT any](ctx context.Context, db bun.IDB, model *ModelT, queryFn func(q *bun.DeleteQuery), befores []hook.Before[ModelT], afters []hook.After[ModelT]) error

func Find

func Find[ModelT any](ctx context.Context, db bun.IDB, queryFn func(q *bun.SelectQuery)) ([]*ModelT, error)

func FindByID

func FindByID[ModelT any](ctx context.Context, db bun.IDB, id any, queryFn func(q *bun.SelectQuery)) (*ModelT, error)

func FindByIDForUpdate

func FindByIDForUpdate[ModelT any](ctx context.Context, db bun.IDB, id any, skipLocked bool, queryFn func(q *bun.SelectQuery)) (*ModelT, error)

func FindFirst

func FindFirst[ModelT any](ctx context.Context, db bun.IDB, queryFn func(q *bun.SelectQuery)) (*ModelT, error)

func SelectForUpdateQuery

func SelectForUpdateQuery[ModelT any](ctx context.Context, db bun.IDB, model *ModelT, skipLocked bool) (*bun.SelectQuery, *schema.Table, error)

func SelectQuery

func SelectQuery[ModelT any](ctx context.Context, db bun.IDB, model *ModelT) (*bun.SelectQuery, *schema.Table, error)

func Trx

func Trx(ctx context.Context, db bun.IDB, fn func(ctx context.Context, tx bun.IDB) error) error

func Update

func Update[ModelT any](ctx context.Context, db bun.IDB, model *ModelT, befores []hook.Before[ModelT], afters []hook.After[ModelT]) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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