cursorpagination

package
v0.0.0-...-30a190e Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPageSize = 20
	MaxPageSize     = 100
)

Variables

This section is empty.

Functions

func BuildPaginatedQuery

func BuildPaginatedQuery(opts *BuildPaginatedQueryOptions) error

BuildPaginatedQuery builds a query with cursor-based pagination

func EncodeCursor

func EncodeCursor(pk PrimaryKey) (string, error)

EncodeCursor creates an encoded cursor from a model's primary key values

Types

type BaseModel

type BaseModel interface {
	GetTableName() string
	GetTableAlias() string
	GetPrimaryKey() PrimaryKey
	GetID() string
}

BaseModel defines required methods for models using cursor pagination

type BuildPaginatedQueryOptions

type BuildPaginatedQueryOptions struct {
	Query          *bun.SelectQuery
	PaginationOpts Query
	Model          BaseModel
}

BuildPaginatedQueryOptions contains options for building paginated queries

type Cursor

type Cursor struct {
	Values map[string]any `json:"values"` // Map of field names to their values
}

Cursor represents a position in the dataset using composite keys

func DecodeCursor

func DecodeCursor(encoded string) (*Cursor, error)

DecodeCursor decodes a cursor string into cursor values

type Cursors

type Cursors struct {
	Next     *string `json:"next,omitempty"`     // Cursor for next page
	Previous *string `json:"previous,omitempty"` // Cursor for previous page
}

Cursors contains pagination cursors for navigation

type Meta

type Meta struct {
	TotalCount int  `json:"totalCount"` // Total number of records
	HasMore    bool `json:"hasMore"`    // Whether there are more records
}

Meta contains metadata about the pagination state

type PrimaryKey

type PrimaryKey struct {
	Fields    []string // Field names in the composite key
	Values    []any    // Values for each field
	Composite bool     // Whether this is a composite key
}

PrimaryKey defines the structure of a composite primary key

type Query

type Query struct {
	Cursor   *string `json:"cursor"`   // Encoded cursor for the current position
	PageSize int     `json:"pageSize"` // Number of items per page
}

Query represents pagination parameters

type Response

type Response[T BaseModel] struct {
	Data    []*T    `json:"data"`    // The paginated data
	Meta    Meta    `json:"meta"`    // Metadata about the response
	Cursors Cursors `json:"cursors"` // Navigation cursors
}

Response represents the paginated response

func ProcessResponse

func ProcessResponse[T BaseModel](
	entities []T,
	total int,
	opts Query,
) (*Response[T], error)

ProcessResponse processes the query results into a paginated response

Jump to

Keyboard shortcuts

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