paginator

package
v0.0.0-...-811544e Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPaginatorLimit = 100

Variables

View Source
var (
	ErrInvalidCursor = errors.New("invalid cursor for paginating")
	ErrInvalidLimit  = errors.New("limit should be greater than 0")
	ErrInvalidModel  = errors.New("entity fields should match rules or keys specified for paginator")
	ErrInvalidOrder  = errors.New("order should be ASC or DESC")
	ErrNoRule        = errors.New("paginator should have at least one rule")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Rules  []Rule
	Keys   []string
	Limit  int
	Order  Order
	After  string
	Before string
	Page   int
}

Config for paginator

func (*Config) Apply

func (c *Config) Apply(p *Paginator)

Apply applies config to paginator

type Cursor

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

func (*Cursor) Cursor

func (c *Cursor) Cursor() *cursor.Cursor

func (*Cursor) Offset

func (c *Cursor) Offset() *Page

func (*Cursor) SetCursor

func (c *Cursor) SetCursor(cur *cursor.Cursor)

func (*Cursor) SetOffset

func (c *Cursor) SetOffset(off *Page)

func (*Cursor) Type

func (c *Cursor) Type() MetaType

type CustomType

type CustomType struct {
	Meta any
	Type reflect.Type
}

CustomType for paginator. It provides extra info needed to paginate across custom types (e.g. JSON)

type MetaType

type MetaType string
const (
	MetaCursor MetaType = "cursor"
	MetaOffset MetaType = "offset"
	MetaNone   MetaType = "none"
)

type Option

type Option interface {
	Apply(p *Paginator)
}

Option for paginator

func WithAfter

func WithAfter(c string) Option

WithAfter configures after cursor for paginator

func WithBefore

func WithBefore(c string) Option

WithBefore configures before cursor for paginator

func WithKeys

func WithKeys(keys ...string) Option

WithKeys configures keys for paginator

func WithLimit

func WithLimit(limit int) Option

WithLimit configures limit for paginator

func WithOrder

func WithOrder(order Order) Option

WithOrder configures order for paginator

func WithPage

func WithPage(page int) Option

WithPage configures the pagination for the specified page

func WithRules

func WithRules(rules ...Rule) Option

WithRules configures rules for paginator

type Order

type Order string

Order type for order

const (
	ASC  Order = "ASC"
	DESC Order = "DESC"
)

Orders

type Page

type Page struct {
	Items        int
	Total        int
	Page         int
	ItemsPerPage int
}

type Paginator

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

Paginator a builder doing pagination

func New

func New(opts ...Option) *Paginator

New creates paginator

func (*Paginator) Paginate

func (p *Paginator) Paginate(ctx context.Context, db SQLSelector, ds *goqu.SelectDataset, dest any) (*Cursor, error)

func (*Paginator) SetAfterCursor

func (p *Paginator) SetAfterCursor(afterCursor string)

SetAfterCursor sets paging after cursor

func (*Paginator) SetBeforeCursor

func (p *Paginator) SetBeforeCursor(beforeCursor string)

SetBeforeCursor sets paging before cursor

func (*Paginator) SetKeys

func (p *Paginator) SetKeys(keys ...string)

SetKeys sets paging keys

func (*Paginator) SetLimit

func (p *Paginator) SetLimit(limit int)

SetLimit sets paging limit

func (*Paginator) SetOrder

func (p *Paginator) SetOrder(order Order)

SetOrder sets paging order

func (*Paginator) SetPage

func (p *Paginator) SetPage(page int)

SetPage sets page number

func (*Paginator) SetRules

func (p *Paginator) SetRules(rules ...Rule)

SetRules sets paging rules

type Rule

type Rule struct {
	Key             string
	Order           Order
	SQLRepr         string
	CustomType      *CustomType
	NULLReplacement any
}

Rule for paginator

type SQLSelector

type SQLSelector interface {
	Select(ctx context.Context, dest any, query string, args ...any) error
	Get(ctx context.Context, dst any, query string, args ...any) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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