Documentation
¶
Overview ¶
Package pagination provides offset and cursor-based pagination support for database queries with filtering integration.
Index ¶
- Constants
- Variables
- func BuildCondition(field, op string, value any) (query.Condition, error)
- func EncodeCursor(c Cursor) string
- func MergeWhereClauses(clauses ...*query.WhereClause) *query.WhereClause
- func ParseFilterKey(key string) (field, op string)
- func ParseFilters(r *http.Request, allowed []string) map[string]any
- func ParseQueryParams(r *http.Request, allowedFilters []string, allowedSorts []string) (PageRequest, map[string]any, []SortField)
- type Cursor
- type FilterBuilder
- type FilterDef
- type PageInfo
- type PageRequest
- type PageResponse
- type PaginationType
- type Paginator
- func (p *Paginator) Execute(ctx context.Context, req PageRequest) (*PageResponse[map[string]any], error)
- func (p *Paginator) WithFields(fields []string) *Paginator
- func (p *Paginator) WithInclude(include []string) *Paginator
- func (p *Paginator) WithOrderBy(orderBy []query.OrderClause) *Paginator
- func (p *Paginator) WithTotal(include bool) *Paginator
- func (p *Paginator) WithWhere(where *query.WhereClause) *Paginator
- type SortField
Constants ¶
const ( // DefaultLimit is the default number of items per page. DefaultLimit = 20 // MaxLimit is the maximum allowed items per page. MaxLimit = 100 // MinLimit is the minimum allowed items per page. MinLimit = 1 )
Variables ¶
var ErrInvalidCursor = errors.New("invalid cursor")
ErrInvalidCursor is returned when cursor decoding fails.
var SupportedOperators = []string{"eq", "ne", "gt", "gte", "lt", "lte", "contains", "in", "startswith", "endswith", "isnull"}
SupportedOperators lists all supported filter operators.
Functions ¶
func BuildCondition ¶
BuildCondition creates a query Condition from field, operator, and value.
func EncodeCursor ¶
EncodeCursor encodes a cursor struct to a base64 URL-safe string.
func MergeWhereClauses ¶
func MergeWhereClauses(clauses ...*query.WhereClause) *query.WhereClause
MergeWhereClauses combines multiple WHERE clauses with AND logic.
func ParseFilterKey ¶
ParseFilterKey parses a filter key in the format field[op] into field and operator. If no operator is specified, defaults to "eq".
func ParseFilters ¶
ParseFilters extracts filter parameters from an HTTP request. Only fields in the allowed list are extracted. Supports both field=value and field[op]=value formats.
func ParseQueryParams ¶
func ParseQueryParams(r *http.Request, allowedFilters []string, allowedSorts []string) (PageRequest, map[string]any, []SortField)
ParseQueryParams extracts both pagination and filter parameters from an HTTP request.
Types ¶
type Cursor ¶
type Cursor struct {
ID string `json:"id"`
Field string `json:"field,omitempty"`
Value any `json:"value,omitempty"`
Direction string `json:"dir,omitempty"`
}
Cursor represents an encoded cursor for keyset pagination.
func DecodeCursor ¶
DecodeCursor decodes a base64 URL-safe string to a cursor struct.
type FilterBuilder ¶
type FilterBuilder struct {
// contains filtered or unexported fields
}
FilterBuilder builds query WHERE clauses from filter parameters.
func NewFilterBuilder ¶
func NewFilterBuilder() *FilterBuilder
NewFilterBuilder creates a new FilterBuilder.
func (*FilterBuilder) Allow ¶
func (b *FilterBuilder) Allow(field string, ops ...string) *FilterBuilder
Allow registers a field with allowed operators. If no operators are specified, all operators are allowed.
func (*FilterBuilder) AllowAll ¶
func (b *FilterBuilder) AllowAll(fields ...string) *FilterBuilder
AllowAll registers multiple fields with all operators allowed.
func (*FilterBuilder) Build ¶
func (b *FilterBuilder) Build(params map[string]any) (*query.WhereClause, error)
Build parses filter parameters and builds a WHERE clause. Parameters should be in the format: field=value or field[op]=value
type PageInfo ¶
type PageInfo struct {
Total *int64 `json:"total,omitempty"`
Page *int `json:"page,omitempty"`
Limit int `json:"limit"`
HasNext bool `json:"has_next"`
HasPrevious bool `json:"has_previous"`
NextCursor string `json:"next_cursor,omitempty"`
PrevCursor string `json:"prev_cursor,omitempty"`
}
PageInfo contains pagination metadata for the response.
type PageRequest ¶
type PageRequest struct {
Type PaginationType
Page int // For offset pagination (1-indexed)
Limit int // Items per page
Cursor string // For cursor pagination (forward)
After string // Alias for cursor (forward pagination)
Before string // For backward pagination
}
PageRequest contains pagination parameters from the request.
func DefaultPageRequest ¶
func DefaultPageRequest() PageRequest
DefaultPageRequest returns a PageRequest with sensible defaults.
func ParsePageRequest ¶
func ParsePageRequest(r *http.Request) PageRequest
ParsePageRequest extracts pagination parameters from an HTTP request.
func (*PageRequest) GetCursor ¶
func (pr *PageRequest) GetCursor() string
GetCursor returns the cursor value, checking both Cursor and After fields.
func (*PageRequest) GetOffset ¶
func (pr *PageRequest) GetOffset() int
GetOffset calculates the offset for offset-based pagination.
func (*PageRequest) IsCursorPagination ¶
func (pr *PageRequest) IsCursorPagination() bool
IsCursorPagination returns true if cursor-based pagination is being used.
func (*PageRequest) Validate ¶
func (pr *PageRequest) Validate()
Validate validates and normalizes the PageRequest.
type PageResponse ¶
PageResponse represents a paginated response with items and pagination metadata.
type PaginationType ¶
type PaginationType string
PaginationType represents the type of pagination to use.
const ( // PaginationOffset uses traditional page/offset pagination. PaginationOffset PaginationType = "offset" // PaginationCursor uses cursor-based (keyset) pagination. PaginationCursor PaginationType = "cursor" )
type Paginator ¶
type Paginator struct {
// contains filtered or unexported fields
}
Paginator executes paginated queries against a database.
func NewPaginator ¶
NewPaginator creates a new Paginator for the given entity.
func (*Paginator) Execute ¶
func (p *Paginator) Execute(ctx context.Context, req PageRequest) (*PageResponse[map[string]any], error)
Execute executes the paginated query based on the request type.
func (*Paginator) WithFields ¶
WithFields sets the fields to select.
func (*Paginator) WithInclude ¶
WithInclude sets the relations to include.
func (*Paginator) WithOrderBy ¶
func (p *Paginator) WithOrderBy(orderBy []query.OrderClause) *Paginator
WithOrderBy sets the ORDER BY clauses for the paginator.