pagination

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MAX_PAGE_SIZE = 1000
	DEFAULT_LIMIT = 50
)

Variables

This section is empty.

Functions

func ApplyCursorToQuery

func ApplyCursorToQuery[T Paginatable](query *bun.SelectQuery, r *CursorRequest, model T, order OrderDirection) (*bun.SelectQuery, error)

ApplyCursorToQuery adds cursor-based pagination to a bun query based on the Paginatable model. The order parameter is of type OrderDirection, ensuring that only valid order directions are used.

func ApplyOffsetToQuery

func ApplyOffsetToQuery(query *bun.SelectQuery, limit, offset int) *bun.SelectQuery

func EncodeCursor

func EncodeCursor(c Cursor) (string, error)

EncodeCursor creates a base64 encoded cursor from a Cursor struct

Types

type Cursor

type Cursor struct {
	SortField   string      `json:"sort_field"`
	SortValue   interface{} `json:"sort_value"`
	UniqueField string      `json:"unique_field"`
	UniqueValue interface{} `json:"unique_value"`
}

Cursor holds the values needed for pagination

func DecodeCursor

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

DecodeCursor decodes a base64 encoded cursor string into a Cursor struct

type CursorRequest

type CursorRequest struct {
	Limit  int            `json:"limit"`
	Cursor string         `json:"cursor"`
	Filter []query.Filter `json:"filters,omitempty"`
}

CursorRequest represents a cursor paginated request

func (*CursorRequest) Validate

func (r *CursorRequest) Validate() error

type CursorResponse

type CursorResponse struct {
	NextCursor  string `json:"next_cursor,omitempty"`
	HasNextPage bool   `json:"has_next_page"`
	Hash        string `json:"hash,omitempty"` // SHA-256 hash of the items for data freshness validation
	// contains filtered or unexported fields
}

CursorResponse represents a cursor paginated response

func BuildCursorResponse

func BuildCursorResponse[T Paginatable](items []T, limit int) (*CursorResponse, error)

BuildCursorResponse processes the query results and creates a paginated response

func (*CursorResponse) GetItems

func (r *CursorResponse) GetItems() interface{}

GetItems returns the underlying items of the response

type OrderDirection added in v1.4.3

type OrderDirection string

OrderDirection represents the allowed direction for pagination.

const (
	ASC  OrderDirection = "asc"
	DESC OrderDirection = "desc"
)

type Paginatable

type Paginatable interface {
	GetSortField() string        // Returns sort field name without table prefix
	GetSortValue() interface{}   // Returns the value for the sort field
	GetUniqueField() string      // Returns unique field name without table prefix
	GetUniqueValue() interface{} // Returns the value for the unique field
}

Paginatable interface that models must implement to use pagination

Jump to

Keyboard shortcuts

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