Documentation ¶
Overview ¶
Package paginator provides a simple paginator implementation for gorm. It also supports configuring the paginator via http.Request query params.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultLimit = 20
DefaultLimit defines the default limit for paginated queries. This is a variable so that users can configure it at runtime.
var DefaultParamNames = ParamNames{"page", "limit", "order"}
DefaultParamNames specifies the query parameter names to use from a http.Request by default when the WithRequest option is uses. This can be overridden at runtime.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(p *paginator)
Option defines the signature of a paginator option function.
func WithLimit ¶
WithLimit configures the limit of the paginator.
paginator.Paginate(db, &v, paginator.WithLimit(10))
func WithOrder ¶
WithOrder configures the order of the paginator.
paginator.Paginate(db, &v, paginator.WithOrder("name DESC", "id"))
func WithPage ¶
WithPage configures the page of the paginator.
paginator.Paginate(db, &v, paginator.WithPage(2))
func WithRequest ¶ added in v0.1.0
func WithRequest(r *http.Request, paramNames ...ParamNames) Option
WithRequest configures the paginator from a *http.Request.
paginator.Paginate(db, &v, paginator.WithRequest(request)) paginator.Paginate(db, &v, paginator.WithRequest(request, paginator.ParamNames{ Page: "page", Limit: "", // Disable limit query param. Order: "order", }))
type Paginator ¶
type Paginator interface { // Paginate takes a value as arguments and returns a paginated result // containing records of the value type. Paginate(interface{}) (*Result, error) }
Paginator defines the interface for a paginator.
type ParamNames ¶
ParamNames defines a type to configure names of query parameters to use from a http.Request. If a field is set to the empty string, it will not be used.
type Result ¶
type Result struct { CurrentPage int `json:"currentPage"` MaxPage int `json:"maxPage"` RecordsPerPage int `json:"recordsPerPage"` TotalRecords int `json:"totalRecords"` Records interface{} `json:"records"` }
Result defines a paginated result.
func Paginate ¶
Paginate is a convenience wrapper for the paginator.
var v []SomeModel res, err := paginator.Paginate(db, &v, paginator.WithPage(2))
func (*Result) IsFirstPage ¶
IsFirstPage returns true if the current page of the result is the first page.
func (*Result) IsLastPage ¶
IsLastPage returns true if the current page of the result is the last page.