pagination

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package pagination provides pagination utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pagination

type Pagination struct {
	Page    int
	PerPage int
}

Pagination holds pagination parameters.

func New

func New(page, perPage int) Pagination

New creates a new Pagination with defaults applied.

func (Pagination) Limit

func (p Pagination) Limit() int

Limit returns the limit for database queries.

func (Pagination) Offset

func (p Pagination) Offset() int

Offset returns the offset for database queries.

type Result

type Result[T any] struct {
	Data       []T   `json:"data"`
	Total      int64 `json:"total"`
	Page       int   `json:"page"`
	PerPage    int   `json:"per_page"`
	TotalPages int   `json:"total_pages"`
}

Result represents a paginated result set.

func NewResult

func NewResult[T any](data []T, total int64, p Pagination) Result[T]

NewResult creates a new paginated Result.

type Sort

type Sort struct {
	Field string
	Order SortOrder
}

Sort represents a sorting specification.

type SortOption

type SortOption struct {
	// contains filtered or unexported fields
}

SortOption represents a parsed sort option with validation.

func NewSortOption

func NewSortOption(allowedFields map[string]string) *SortOption

NewSortOption creates a new SortOption with allowed fields. allowedFields maps user-facing field names to database column names. Example: {"created_at": "created_at", "name": "name", "updated_at": "updated_at"}

func (*SortOption) IsEmpty

func (s *SortOption) IsEmpty() bool

IsEmpty returns true if no sorts are specified.

func (*SortOption) Parse

func (s *SortOption) Parse(sortStr string) *SortOption

Parse parses a sort string and validates fields. Format: "-created_at,name" means ORDER BY created_at DESC, name ASC Prefix "-" means descending order.

func (*SortOption) SQL

func (s *SortOption) SQL() string

SQL returns the ORDER BY clause without the "ORDER BY" prefix. Returns empty string if no sorts. Example: "created_at DESC, name ASC"

func (*SortOption) SQLWithDefault

func (s *SortOption) SQLWithDefault(defaultSort string) string

SQLWithDefault returns the ORDER BY clause, using defaultSort if no sorts specified.

func (*SortOption) Sorts

func (s *SortOption) Sorts() []Sort

Sorts returns the parsed sort specifications.

type SortOrder

type SortOrder string

SortOrder represents the sort direction.

const (
	SortAsc  SortOrder = "ASC"
	SortDesc SortOrder = "DESC"
)

Jump to

Keyboard shortcuts

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