Help us shape the future of Go! Take the 2026 Go Developer Survey

pagination

package
v2.17.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const UnknownTotal int64 = -1

UnknownTotal is the sentinel returned in TotalItems/TotalPages when SkipCount is set.

Variables

This section is empty.

Functions

func ApplyBooleanFilter

func ApplyBooleanFilter(q *gorm.DB, column, value string) *gorm.DB

ApplyBooleanFilter adds a WHERE clause for boolean columns. It detects comma-separated values and maps "true"/"1" to true and "false"/"0" to false.

func ApplyFilter

func ApplyFilter(q *gorm.DB, column, value string) *gorm.DB

ApplyFilter adds a WHERE clause to the GORM query. It detects comma-separated values and uses IN (?) for multiple values, or = ? for single values.

func ApplyLikeSearch

func ApplyLikeSearch(q *gorm.DB, search, condition string) *gorm.DB

ApplyLikeSearch adds a LIKE search condition using the same wildcard pattern for every placeholder in condition.

Types

type Config

type Config[T any] struct {
	SearchAccessors []SearchAccessor[T]
	// SortBindings order matters: the first binding is the default sort when the
	// requested key is empty or unknown, and serves as the tie-break for every
	// other binding so pagination is deterministic.
	SortBindings    []SortBinding[T]
	FilterAccessors []FilterAccessor[T]
}

func WithEnvironment added in v2.16.1

func WithEnvironment[T Environmental](c Config[T]) Config[T]

func (Config[T]) MatchesSearchAndFilters added in v2.10.0

func (c Config[T]) MatchesSearchAndFilters(item T, params QueryParams) bool

MatchesSearchAndFilters reports whether a single item passes params' search term and filters under the config — the same predicate SearchOrderAndPaginate applies. It lets callers that decode large payloads incrementally drop non-matching items as they go instead of materializing everything first.

func (Config[T]) OrderAndPaginate added in v2.10.0

func (c Config[T]) OrderAndPaginate(items []T, params QueryParams) []T

OrderAndPaginate sorts items and slices the requested page without applying the search term or filters — for callers that already filtered while building the item list (e.g. dropping non-matches during an incremental decode) and only need ordering plus the page cut.

func (Config[T]) SearchOrderAndPaginate added in v2.10.0

func (c Config[T]) SearchOrderAndPaginate(items []T, params QueryParams) FilterResult[T]

type EnvironmentPage added in v2.16.1

type EnvironmentPage[T any] struct {
	Environment    base.EnvironmentRef
	Items          []T
	TotalAvailable int64
}

type Environmental added in v2.16.1

type Environmental interface {
	Environment() base.EnvironmentRef
}

type FilterAccessor

type FilterAccessor[T any] struct {
	Key string
	Fn  func(item T, filterValue string) bool
	// NoSplit passes the raw value through instead of treating commas as alternatives.
	NoSplit bool
}

type FilterResult

type FilterResult[T any] struct {
	Items          []T
	TotalCount     int64
	TotalAvailable int64
}

type Params

type Params struct {
	Start int
	Limit int
	// SkipCount opts out of the COUNT(*) query that backs TotalItems/TotalPages.
	// When true, paginated DB helpers return UnknownTotal (-1) for both fields and
	// the response is suitable for infinite-scroll UIs that don't render page numbers.
	// Default (false) preserves the historical behaviour for all existing callers.
	SkipCount bool
}

type QueryParams

type QueryParams struct {
	SearchQuery
	SortParams
	Params

	Filters map[string]string
}

func (QueryParams) PaginateSortAndMapDB added in v2.10.0

func (params QueryParams) PaginateSortAndMapDB[M, D any](query *gorm.DB, records *[]M) ([]D, Response, error)

PaginateSortAndMapDB paginates DB records and maps them to API DTOs.

type Response

type Response struct {
	TotalPages      int64 `json:"totalPages"`
	TotalItems      int64 `json:"totalItems"`
	CurrentPage     int   `json:"currentPage"`
	ItemsPerPage    int   `json:"itemsPerPage"`
	GrandTotalItems int64 `json:"grandTotalItems,omitempty"`
}

func BuildResponse added in v2.8.0

func BuildResponse(totalCount, totalAvailable int64, params QueryParams) Response

BuildResponse creates a pagination Response from raw counts. Handles the special case where limit = -1 means "show all".

func MergeEnvironmentPages added in v2.16.1

func MergeEnvironmentPages[T Environmental, PT interface {
	*T
	SetEnvironment(base.EnvironmentRef)
}](config Config[T], pages []EnvironmentPage[T], params QueryParams) ([]T, Response)

func PaginateAndSortDB

func PaginateAndSortDB[M any](params QueryParams, query *gorm.DB, result *[]M) (Response, error)

type SearchAccessor

type SearchAccessor[T any] = func(T) (string, error)

SearchAccessor extracts a searchable string from T. Return any error to skip the field (e.g. when matching an unknown enum state).

Note: returning ("", nil) will match!

type SearchQuery

type SearchQuery struct {
	Search string
}

type SortBinding

type SortBinding[T any] struct {
	Key    string
	Fn     SortOption[T]
	DescFn SortOption[T]
}

type SortOption

type SortOption[T any] func(a, b T) int

type SortOrder

type SortOrder string
const (
	SortAsc  SortOrder = "asc"
	SortDesc SortOrder = "desc"
)

type SortParams

type SortParams struct {
	Sort  string
	Order SortOrder
}

Jump to

Keyboard shortcuts

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