Documentation
¶
Index ¶
- Constants
- func ApplyBooleanFilter(q *gorm.DB, column string, value string) *gorm.DB
- func ApplyFilter(q *gorm.DB, column string, value string) *gorm.DB
- func ApplyLikeSearch(q *gorm.DB, search string, condition string) *gorm.DB
- type Config
- type FilterAccessor
- type FilterResult
- type Params
- type QueryParams
- type Response
- type SearchAccessor
- type SearchQuery
- type SortBinding
- type SortOption
- type SortOrder
- type SortParams
Constants ¶
const UnknownTotal int64 = -1
UnknownTotal is the sentinel returned in TotalItems/TotalPages when SkipCount is set.
Variables ¶
This section is empty.
Functions ¶
func ApplyBooleanFilter ¶
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 ¶
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 FilterResult ¶
func SearchOrderAndPaginate ¶
func SearchOrderAndPaginate[T any](items []T, params QueryParams, searchConfig Config[T]) FilterResult[T]
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
}
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 PaginateSortAndMapDB ¶
func PaginateSortAndMapDB[M any, D any](params QueryParams, query *gorm.DB, records *[]M) ([]D, Response, error)
PaginateSortAndMapDB paginates DB records and maps them to API DTOs.
type SearchAccessor ¶
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] }