Documentation
¶
Overview ¶
Package pagination provides a paginated result envelope with page metadata.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CursorPage ¶ added in v1.1.0
type CursorPage[T any] struct { Data []T `json:"data"` NextCursor string `json:"next_cursor"` // "" = no next page PrevCursor string `json:"prev_cursor"` // "" = no previous page }
CursorPage is a single keyset (cursor) page together with the cursors needed to navigate to the next and previous pages. An empty cursor means there is no page in that direction.
func NewCursorPage ¶ added in v1.1.0
func NewCursorPage[T any](rows []T, kq parser.KeysetQuery, cols []parser.KeysetColumn, extract func(T) []any) *CursorPage[T]
NewCursorPage builds a CursorPage from the rows returned by a parser.Keyset query (which fetched LIMIT+1 rows). It trims the sentinel extra row, restores display order for backward pages, and derives the Next/Prev cursors from the edge rows using extract, which must return the keyset column values for a row in the same order as cols.
Example ¶
Build a keyset (cursor) page from the rows a parser.Keyset query returns. The query fetches LIMIT+1 rows; NewCursorPage trims the sentinel and derives the navigation cursors. Here limit is 2 and three rows came back, so a next page exists; with no incoming cursor this is the first page, so there is no previous one.
package main
import (
"fmt"
"github.com/rahmadafandi/fibr/pagination"
"github.com/rahmadafandi/fibr/parser"
)
func main() {
type article struct {
ID int64
Title string
}
rows := []article{{1, "first"}, {2, "second"}, {3, "third"}}
cols := []parser.KeysetColumn{{Name: "id"}}
kq := parser.KeysetQuery{Limit: 2}
page := pagination.NewCursorPage(rows, kq, cols, func(a article) []any {
return []any{a.ID}
})
fmt.Println("rows:", len(page.Data))
fmt.Println("has next:", page.NextCursor != "")
fmt.Println("has prev:", page.PrevCursor != "")
}
Output: rows: 2 has next: true has prev: false
func (*CursorPage[T]) LinkHeader ¶ added in v1.4.0
func (p *CursorPage[T]) LinkHeader(baseURL string) string
LinkHeader returns an RFC 5988 Link header value pointing at the next and previous pages, derived from the cursors and baseURL (the request URL without the cursor/before params). It returns "" when there is neither a next nor a previous page. Set it with c.Set("Link", page.LinkHeader(url)).
type Pagination ¶
type Pagination[T any] struct { Data []T `json:"data"` PageSize int `json:"page_size"` Count int `json:"count"` TotalCount int `json:"total_count"` PageCount int `json:"page_count"` PageNumber int `json:"page_number"` StartNumber int `json:"start_number"` }
Pagination holds a single page of results together with the metadata needed to render page navigation controls.
func NewPagination ¶
func NewPagination[T any](items []T, pageSize int, pageNumber int, totalCount int) *Pagination[T]
NewPagination constructs a Pagination value from a slice of items and the associated paging parameters. pageNumber is clamped to 1 if less than 1.
Example ¶
Build a page envelope from a slice of items plus the paging parameters. The derived metadata (PageCount, StartNumber, ...) is computed for you.
package main
import (
"fmt"
"github.com/rahmadafandi/fibr/pagination"
)
func main() {
items := []string{"alice", "bob", "carol"}
page := pagination.NewPagination(items, 10, 1, 23)
fmt.Println("page:", page.PageNumber)
fmt.Println("size:", page.PageSize)
fmt.Println("on this page:", page.Count)
fmt.Println("total:", page.TotalCount)
fmt.Println("total pages:", page.PageCount)
}
Output: page: 1 size: 10 on this page: 3 total: 23 total pages: 3