bucko

package module
v0.0.0-...-cf09f61 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: MIT Imports: 14 Imported by: 0

README

bucko

simple middleware for integrating bun into an echo api

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultRel = &RelApplier{UseDefault: true}
	IgnoredRel = &RelApplier{Ignore: true}
)
View Source
var DB db

Functions

func BaseCheckExists

func BaseCheckExists(model interface{}, ctx context.Context, query string, args ...interface{}) (exists bool, err error)

func BaseCheckPKExists

func BaseCheckPKExists(model interface{}, ctx context.Context) (exists bool, err error)

func GetPK

func GetPK(m interface{}) uint64

func GetPKCol

func GetPKCol(m interface{}) bun.Ident

func Middleware

func Middleware(next echo.HandlerFunc) echo.HandlerFunc

Middleware provides echo middleware for `echo.Echo.Use()`

func SetJWTConfig

func SetJWTConfig(config middleware.JWTConfig)

SetJWTConfig sets the primary JWT config from an echo.middleware.JWTConfig instance.

func SetUserGetter

func SetUserGetter(userGetter func(claims jwt.Claims) (u BaseFieldModel, err error))

SetUserGetter takes a function to transform claims into a user as a `BaseFieldModel`.

func UseDB

func UseDB(db *bun.DB)

Types

type BaseFieldModel

type BaseFieldModel interface {

	// GetSelectQuery must return a SelectQuery (excluding relations) suited for selecting the model based off of cq.Q.
	GetSelectQuery(cq *CtxQuery) *bun.SelectQuery

	// Insert must insert itself and the result or an error.
	Insert(rc *ReqCtx) (BaseFieldModel, error)

	// GetColumn must return the column name of the main field used to retrieve the model (not PK).
	GetColumn() bun.Ident

	// GetField must return the field used to retrieve the model (not PK).
	GetField() interface{}

	// GetParam must return the URL param name of the main field used to retrieve the model (not PK).
	GetParam() string
}

BaseFieldModel provides an interface to models commonly referenced to help provide shortcuts.

type CtxQuery

type CtxQuery struct {
	*bun.SelectQuery
	R          *ReqCtx
	M          BaseFieldModel
	JoinPrefix string
	TableAlias bun.Safe
}

func (*CtxQuery) ApplySearch

func (cq *CtxQuery) ApplySearch(searchCols bun.Safe)

func (*CtxQuery) CheckExistsFromField

func (cq *CtxQuery) CheckExistsFromField(field interface{}) (exists bool, err error)

func (*CtxQuery) CheckExistsFromPK

func (cq *CtxQuery) CheckExistsFromPK(id uint64) (exists bool, err error)

func (*CtxQuery) CheckFieldExists

func (cq *CtxQuery) CheckFieldExists() (exists bool, err error)

func (*CtxQuery) CheckPKExists

func (cq *CtxQuery) CheckPKExists() (exists bool, err error)

func (*CtxQuery) ColAlias

func (cq *CtxQuery) ColAlias(column bun.Ident) bun.Ident

ColAlias returns a query's column's alias for selection to bun, safe to use in joins.

func (*CtxQuery) DeleteFromPK

func (cq *CtxQuery) DeleteFromPK() (err error)

func (*CtxQuery) FromField

func (cq *CtxQuery) FromField() (err error)

func (*CtxQuery) FromPK

func (cq *CtxQuery) FromPK() (err error)

func (*CtxQuery) FromParam

func (cq *CtxQuery) FromParam() (err error)

func (*CtxQuery) GetFromField

func (cq *CtxQuery) GetFromField(field interface{}) (err error)

func (*CtxQuery) GetFromPK

func (cq *CtxQuery) GetFromPK(id uint64) (err error)

func (*CtxQuery) Insert

func (cq *CtxQuery) Insert() (err error)

Insert inserts CtxQuery's model based on its Insert() method.

func (*CtxQuery) Q

func (cq *CtxQuery) Q(q *bun.SelectQuery)

Q updates the query.

func (*CtxQuery) SafeCol

func (cq *CtxQuery) SafeCol(column bun.Ident) bun.Safe

SafeCol returns a query's column, safe to use in joins.

func (*CtxQuery) Select

func (cq *CtxQuery) Select()

Select applies its model's GetSelectQuery and relations to itself.

func (*CtxQuery) SetLimitOffset

func (cq *CtxQuery) SetLimitOffset()

SetLimitOffset sets a queries limit & offset from given URL parameters.

func (*CtxQuery) SetPK

func (cq *CtxQuery) SetPK() (err error)

func (*CtxQuery) WhereParamToCol

func (cq *CtxQuery) WhereParamToCol()

type CustomRelI

type CustomRelI interface {
	BaseFieldModel
	GetCustomRel(rc *ReqCtx, rel *schema.Relation) *RelApplier
}

CustomRelI provides functionality to customize relations to a BaseFieldModel. To disable a relation, return nil.

type DefaultJwtClaims

type DefaultJwtClaims struct {
	User *DefaultUser `json:"user"`
	jwt.StandardClaims
}

type DefaultUser

type DefaultUser struct {
	bun.BaseModel `bun:"users,alias:user"`
	Username      string `json:"username" bun:"username"`
	Id            uint64 `json:"-" bun:"id,pk"`
}

DefaultUser is the Default user for parsing JWT.

func (*DefaultUser) GetColumn

func (*DefaultUser) GetColumn() bun.Ident

func (*DefaultUser) GetField

func (m *DefaultUser) GetField() interface{}

func (*DefaultUser) GetParam

func (*DefaultUser) GetParam() string

func (*DefaultUser) GetSelectQuery

func (*DefaultUser) GetSelectQuery(cq *CtxQuery) *bun.SelectQuery

func (*DefaultUser) Insert

func (b *DefaultUser) Insert(rc *ReqCtx) (m BaseFieldModel, err error)

type RelApplier

type RelApplier struct {
	UseDefault  bool
	Ignore      bool
	ApplyFunc   RelApplyFunc
	FollowModel BaseFieldModel
}

RelApplier can customize how to handle a relation.

type RelApplyFunc

type RelApplyFunc func(q *bun.SelectQuery) *bun.SelectQuery

type ReqCtx

type ReqCtx struct {
	echo.Context
	Ctx    context.Context
	UserId nulls.UInt32
}

func NewCtx

func NewCtx(c echo.Context, ctx context.Context) *ReqCtx

NewCtx creates a new ReqCtx instance from an echo and context Context object. Uses context.Background if ctx is nil.

func (*ReqCtx) AuthorInsert

func (rc *ReqCtx) AuthorInsert(q *bun.InsertQuery) *bun.InsertQuery

AuthorInsert sets the user_id column to the request's user id on insert statements.

func (*ReqCtx) Query

func (rc *ReqCtx) Query(m BaseFieldModel) *CtxQuery

func (*ReqCtx) Search

func (rc *ReqCtx) Search(q *bun.SelectQuery, searchCols bun.Safe) *bun.SelectQuery

Jump to

Keyboard shortcuts

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