pagination

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package pagination provides a generic cursor-based pagination implementation. It handles direction detection, n+1 pattern processing, and cursor building for stores that need paginated list operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config[T any] struct {
	Limit int
	Order string // "asc" or "desc" - user's requested order
	Next  string // next cursor (empty if none)
	Prev  string // prev cursor (empty if none)

	Fetch  func(context.Context, QueryInput) ([]T, error)
	Cursor Cursor[T]
}

Config contains all parameters for a paginated query.

type Cursor

type Cursor[T any] struct {
	Encode func(T) string               // item → cursor string
	Decode func(string) (string, error) // cursor string → position for query
}

Cursor groups encode/decode functions for cursor serialization. These are paired together to support versioning and backward compatibility.

type Direction

type Direction int

Direction indicates whether pagination is moving forward or backward.

const (
	Forward Direction = iota
	Backward
)

type QueryInput

type QueryInput struct {
	Limit     int
	Compare   string // "<" or ">" - cursor comparison operator
	SortDir   string // "asc" or "desc" - query sort direction (may differ from user's Order)
	CursorPos string // decoded cursor position (empty if first page)
}

QueryInput is passed to the Fetch function with computed query parameters.

type Result

type Result[T any] struct {
	Items []T
	Next  string // empty if no more pages ahead
	Prev  string // empty if no more pages behind (or first page)
}

Result contains the paginated results and cursors.

func Run

func Run[T any](ctx context.Context, cfg Config[T]) (*Result[T], error)

Run executes a paginated query.

Directories

Path Synopsis
Package paginationtest provides a reusable test suite for cursor-based pagination.
Package paginationtest provides a reusable test suite for cursor-based pagination.

Jump to

Keyboard shortcuts

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