pagination

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPageSize = 25

DefaultPageSize is the default number of items per page.

View Source
const MaxPageSize = 100

MaxPageSize is the maximum allowed page size.

Variables

This section is empty.

Functions

func DecodeCursor

func DecodeCursor(cursor string) (field string, value string, err error)

DecodeCursor decodes a base64 cursor string into its field and value components.

func DecodeMultiCursor

func DecodeMultiCursor(cursor string) ([]multiCursorField, error)

DecodeMultiCursor returns the ordered list of (column, value) pairs the cursor encoded. Returns the empty slice + an error if the cursor doesn't match the expected shape.

func EncodeCursor

func EncodeCursor(field string, value any) string

EncodeCursor creates a base64-encoded opaque cursor from a field name and value.

func EncodeMultiCursor

func EncodeMultiCursor(fields []string, row map[string]any) string

EncodeMultiCursor builds an opaque cursor from an ordered list of (column, value) pairs. Used for composite cursor pagination — ORDER BY composes the fields in the same order, and the WHERE clause becomes a tuple comparison "(c1, c2, …) > ($1, $2, …)".

func ParseCursorPagination

func ParseCursorPagination(r *http.Request) (cursor string, limit int, direction string)

ParseCursorPagination extracts cursor, limit, and direction from query parameters. Direction defaults to "forward"; can be set via ?direction=backward.

func ParsePagination

func ParsePagination(r *http.Request) (cursor string, limit int, offset int)

ParsePagination extracts cursor, limit, and offset from query parameters. If cursor is present, offset will be 0 (cursor takes precedence).

Types

type CursorPage

type CursorPage struct {
	Data    []map[string]any `json:"data"`
	Cursor  string           `json:"cursor"`
	HasMore bool             `json:"hasMore"`
	Total   int              `json:"total,omitempty"`
}

CursorPage represents a page of results using cursor-based pagination.

func NewCursorPage

func NewCursorPage(data []map[string]any, cursorField string, limit int) CursorPage

NewCursorPage builds a CursorPage from data. It fetches limit+1 rows to determine HasMore, and encodes the next cursor from the last row's cursorField.

type OffsetPage

type OffsetPage struct {
	Data       []map[string]any `json:"data"`
	Page       int              `json:"page"`
	PageSize   int              `json:"pageSize"`
	Total      int              `json:"total"`
	TotalPages int              `json:"totalPages"`
}

OffsetPage represents a page of results using offset-based pagination.

func NewOffsetPage

func NewOffsetPage(data []map[string]any, page, pageSize, total int) OffsetPage

NewOffsetPage builds an OffsetPage with computed TotalPages from total and pageSize.

Jump to

Keyboard shortcuts

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