pagination

package
v1.15.3 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyBooleanFilter

func ApplyBooleanFilter(q *gorm.DB, column string, 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 string, 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.

Types

type Config

type Config[T any] struct {
	SearchAccessors []SearchAccessor[T]
	SortBindings    []SortBinding[T]
	FilterAccessors []FilterAccessor[T]
}

type FilterAccessor

type FilterAccessor[T any] struct {
	Key string
	Fn  func(item T, filterValue string) bool
}

type FilterResult

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

func SearchOrderAndPaginate

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

type PaginationParams

type PaginationParams struct {
	Start int
	Limit int
}

type QueryParams

type QueryParams struct {
	SearchQuery
	SortParams
	PaginationParams
	Filters map[string]string
}

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 BuildResponseFromFilterResult

func BuildResponseFromFilterResult[T any](result FilterResult[T], params QueryParams) Response

BuildResponseFromFilterResult creates a pagination Response from a FilterResult. Handles the special case where limit = -1 means "show all".

func PaginateAndSortDB

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

type SearchAccessor

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

Return any error to skip the field (for when matching an unknown state on an enum)

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

type SearchQuery

type SearchQuery struct {
	Search string
}

type SortBinding

type SortBinding[T any] struct {
	Key string
	Fn  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