brest

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 18 Imported by: 0

README

brest

Rest API with Bun

Documentation

Index

Constants

View Source
const (
	// Form content type
	Form string = "application/x-www-form-urlencoded"
	// Json content type
	Json = "application/json"
	// Msgpack content type
	Msgpack = "application/x-msgpack"
)

Variables

This section is empty.

Functions

func ContextWithDb

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

ContextWithDb sets Db to context request

func ContextWithTx

func ContextWithTx(ctx context.Context, tx *bun.Tx) context.Context

ContextWithTx sets Tx to context request

func ContextWithValue

func ContextWithValue(ctx context.Context, keyName string, value interface{}) context.Context

ContextWithValue sets Value to context request

func DbFromContext

func DbFromContext(ctx context.Context) *bun.DB

DbFromContext retrives Db from context

func Execute

func Execute(ctx context.Context, execFunc ExecFunc) error

Execute executes ExecFunc in transaction

func ExecuteWithPropagation

func ExecuteWithPropagation(ctx context.Context, propagation Propagation, execFunc ExecFunc) error

ExecuteWithPropagation executes ExecFunc in transaction with specific propagation

func TxFromContext

func TxFromContext(ctx context.Context) *bun.Tx

TxFromContext retrives Tx from context

func ValueFromContext

func ValueFromContext(ctx context.Context, keyName string) interface{}

ValueFromContext retrives Value from context

func Version

func Version() string

Version is the current release version.

Types

type Action

type Action int

Action type

const (
	// Get action
	Get Action = 1 << iota
	// Post action
	Post
	// Put action
	Put
	// Patch action
	Patch
	// Delete action
	Delete
)
const All Action = Get + Post + Put + Patch + Delete

All actions

const None Action = 0

None action

func (Action) String

func (a Action) String() string

type AfterHook added in v1.0.2

type AfterHook func(ctx context.Context, restQuery *RestQuery, entity interface{}) error

AfterHook defines after execution callback function

type BeforeHook added in v1.0.2

type BeforeHook func(ctx context.Context, restQuery *RestQuery, entity interface{}) error

BeforeHook defines before execution callback function

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config structure

func NewConfig

func NewConfig(prefix string, db *bun.DB) *Config

NewConfig constructs Config

func (*Config) AddResource

func (c *Config) AddResource(resource *Resource)

AddResource adds resource

func (*Config) DB

func (c *Config) DB() *bun.DB

DB gets db

func (*Config) DefaultAccept

func (c *Config) DefaultAccept() string

DefaultAccept gets defaultAccept

func (*Config) DefaultContentType

func (c *Config) DefaultContentType() string

DefaultContentType gets defaultContentType

func (*Config) ErrorLogger

func (c *Config) ErrorLogger() *log.Logger

ErrorLogger gets error logger

func (*Config) GetResource

func (c *Config) GetResource(resourceName string) *Resource

GetResource gets resource

func (*Config) InfoLogger

func (c *Config) InfoLogger() *log.Logger

InfoLogger gets info logger

func (*Config) Prefix

func (c *Config) Prefix() string

Prefix gets prefix

func (*Config) SetDefaultAccept

func (c *Config) SetDefaultAccept(defaultAccept string)

SetDefaultAccept sets defaultAccept

func (*Config) SetDefaultContentType

func (c *Config) SetDefaultContentType(defaultContentType string)

SetDefaultContentType sets defaultContentType

func (*Config) SetErrorLogger

func (c *Config) SetErrorLogger(logger *log.Logger)

SetErrorLogger sets error logger

func (*Config) SetInfoLogger

func (c *Config) SetInfoLogger(logger *log.Logger)

SetInfoLogger sets info logger

func (*Config) SetPrefix

func (c *Config) SetPrefix(prefix string)

SetPrefix sets prefix

func (*Config) String

func (c *Config) String() string

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine structure

func NewEngine

func NewEngine(config *Config) *Engine

NewEngine constructs Engine

func (*Engine) Config

func (e *Engine) Config() *Config

Config gets config

func (*Engine) Deserialize

func (e *Engine) Deserialize(restQuery *RestQuery, resource *Resource, entity interface{}) error

Deserialize deserializes data into entity

func (*Engine) Execute

func (e *Engine) Execute(restQuery *RestQuery) (interface{}, error)

Execute executes a rest query

type Error

type Error struct {
	Message string
	Cause   error
	Code    int
}

Error struct

func NewErrorBadRequest

func NewErrorBadRequest(message string) *Error

NewErrorBadRequest constructs Error with bad request code

func NewErrorForbbiden

func NewErrorForbbiden(message string) *Error

NewErrorForbbiden constructs Error with forbidden code

func NewErrorFromCause

func NewErrorFromCause(cause error) *Error

NewErrorFromCause constructs Error from cause error

func (Error) Error

func (e Error) Error() string

Error implements the error interface

func (Error) StatusCode

func (e Error) StatusCode() int

StatusCode returns code

type ExecFunc

type ExecFunc func(ctx context.Context, tx *bun.Tx) error

ExecFunc definition

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

Executor structure

func NewExecutor

func NewExecutor(config *Config, restQuery *RestQuery, entity interface{}) *Executor

NewExecutor constructs Executor

func (*Executor) DeleteExecFunc

func (e *Executor) DeleteExecFunc() ExecFunc

DeleteExecFunc deletes execution function

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, execFunc ExecFunc) error

Execute executes query

func (*Executor) GetOneExecFunc

func (e *Executor) GetOneExecFunc() ExecFunc

GetOneExecFunc gets one execution function

func (*Executor) GetSliceExecFunc

func (e *Executor) GetSliceExecFunc() ExecFunc

GetSliceExecFunc gets slice execution function

func (*Executor) InsertExecFunc

func (e *Executor) InsertExecFunc() ExecFunc

InsertExecFunc inserts execution function

func (*Executor) UpdateExecFunc

func (e *Executor) UpdateExecFunc() ExecFunc

UpdateExecFunc updates execution function

type Field

type Field struct {
	Name string
}

Field structure

func (*Field) String

func (f *Field) String() string

type Filter

type Filter struct {
	Op      Op          // operation
	Attr    string      // attribute name
	Value   interface{} // attribute value
	Filters []*Filter   // sub filters for 'and' and 'or' operations
}

Filter structure

func (*Filter) String

func (f *Filter) String() string

type Op

type Op string

Op operation filter type

const (
	// And operation for group
	And Op = "and"
	// Or operation for group
	Or Op = "or"
	// Eq operation for attribute (? = ?)
	Eq Op = "eq"
	// Neq operation for attribute (? != ?)
	Neq Op = "neq"
	// In operation for attribute (? IN ?)
	In Op = "in"
	// Nin operation for attribute (? NOT IN ?)
	Nin Op = "nin"
	// Gt operation for attribute (? > ?)
	Gt Op = "gt"
	// Gte operation for attribute (? >= ?)
	Gte Op = "gte"
	// Lt operation for attribute (? < ?)
	Lt Op = "lt"
	// Lte operation for attribute (? <= ?)
	Lte Op = "lte"
	// Lk operation for attribute (? LIKE ?)
	Lk Op = "lk"
	// Nlk operation for attribute (? NOT LIKE ?)
	Nlk Op = "nlk"
	// Ilk operation for attribute (? LOWER LIKE ?)
	Llk Op = "ilk"
	// Nilk operation for attribute (? NOT LOWER LIKE ?)
	Nllk Op = "nilk"
	// Sim operation for attribute (? SIMILAR TO ?)
	Sim Op = "sim"
	// Nsim operation for attribute (? NOT SIMILAR TO ?)
	Nsim Op = "nsim"
	// Lulk operation for attribute (? LOWER UNACCENT LIKE ?)
	Lulk Op = "ilkua"
	// Nlulk operation for attribute (? NOT LOWER UNACCENT ILIKE ?)
	Nlulk Op = "nilkua"
	// Null operation for attribute (? IS NULL)
	Null Op = "null"
	// Nnull operation for attribute (? IS NOT NULL)
	Nnull Op = "nnull"
)

func (Op) String

func (o Op) String() string

type Page

type Page struct {
	Slice  interface{} `json:"slice"`
	Offset int         `json:"offset"`
	Limit  int         `json:"limit"`
	Count  int         `json:"count"`
}

Page structure

func NewPage

func NewPage(slice interface{}, count int, restQuery *RestQuery) *Page

NewPage constructs Page

type Propagation

type Propagation string

Propagation type

const (
	// Current supports a current transaction, creates a new one if none exists.
	Current Propagation = "Current"

	// Mandatory needs a current transaction, return an exception if none exists
	Mandatory Propagation = "Mandatory"

	// Savepoint supports a current transaction, creates a new one if none exists, creates savepoint and never return propagation error
	Savepoint Propagation = "Savepoint"
)

type Relation

type Relation struct {
	Name string
}

Relation structure

func (*Relation) String

func (r *Relation) String() string

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

Resource structure

func NewResource

func NewResource(name string, entity interface{}, action Action) *Resource

NewResource constructs Resource

func NewResourceWithHooks added in v1.0.2

func NewResourceWithHooks(name string, entity interface{}, action Action, beforeHook BeforeHook, afterHook AfterHook) *Resource

NewResourceWithHooks constructs Resource with hooks

func (*Resource) Action

func (r *Resource) Action() Action

Action returns action

func (*Resource) Name

func (r *Resource) Name() string

Name returns name

func (*Resource) ResourceType

func (r *Resource) ResourceType() reflect.Type

ResourceType returns resourceType

func (*Resource) String

func (r *Resource) String() string

type RestQuery

type RestQuery struct {
	Request     *http.Request
	Action      Action
	Resource    string
	Key         string
	ContentType string
	Accept      string
	Content     interface{}
	Offset      int
	Limit       int
	Fields      []*Field
	Relations   []*Relation
	Sorts       []*Sort
	Filter      *Filter
	Debug       bool
}

RestQuery structure

func RequestDecoder

func RequestDecoder(request *http.Request, config *Config) *RestQuery

RequestDecoder decodes rest parameters from request

func (*RestQuery) String

func (q *RestQuery) String() string

type Server

type Server struct {
	Engine
	// contains filtered or unexported fields
}

Server structure

func NewServer

func NewServer(config *Config) *Server

NewServer constructs Server

func (*Server) Serialize

func (s *Server) Serialize(restQuery *RestQuery, entity interface{}) ([]byte, string, error)

Serialize serializes data into entity

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP serves rest request

func (*Server) SetNextHandler

func (s *Server) SetNextHandler(next http.Handler)

SetNextHandler sets next handler for middleware use

type Sort

type Sort struct {
	Name string
	Asc  bool
}

Sort structure

func (*Sort) String

func (s *Sort) String() string

Jump to

Keyboard shortcuts

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