Documentation
¶
Overview ¶
Package cursor uses a reference point (cursor) to fetch the next set of results. This reference point is typically a unique identifier that define the sort order.
Index ¶
- func Encrypt[T Pointer](c *Cursor[T], secret []byte) ([]byte, error)
- type Cursor
- func Decrypt[T Pointer](content, secret []byte) (*Cursor[T], error)
- func First[T Pointer](c *Cursor[T]) *Cursor[T]
- func Last[T Pointer](c *Cursor[T]) *Cursor[T]
- func New[T Pointer](limit, total int) *Cursor[T]
- func Next[T Pointer](c *Cursor[T]) *Cursor[T]
- func Prev[T Pointer](c *Cursor[T]) *Cursor[T]
- func (c *Cursor[T]) Add(d T)
- func (c *Cursor[T]) Decode(text []byte) error
- func (c *Cursor[T]) Encode() ([]byte, error)
- func (c *Cursor[T]) IsExpired(maxAge time.Duration) bool
- func (c *Cursor[T]) Reset()
- func (c *Cursor[T]) String() string
- func (c *Cursor[T]) TotalItems() int
- func (c *Cursor[T]) TotalPages() int
- type Int64
- type List
- type Pagination
- type Pointer
- type Statement
- type String
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cursor ¶
type Cursor[T Pointer] struct { Prev *T `json:"prev,omitempty"` Next *T `json:"next,omitempty"` IssuedAt int64 `json:"issued_at,omitempty"` // epoch seconds Limit int `json:"limit"` Total *int `json:"total,omitempty"` Filters url.Values `json:"filters,omitempty"` // contains filtered or unexported fields }
Cursor contains elements required to paginate based on a cursor, a data pointed the start of the data to list.
func (*Cursor[T]) Add ¶
func (c *Cursor[T]) Add(d T)
Add notifies a new entry to the managed list of result.
func (*Cursor[T]) IsExpired ¶
IsExpired returns true if the issued timestamp exceeds the max age allowed.
func (*Cursor[T]) Reset ¶
func (c *Cursor[T]) Reset()
Reset resets the cursor allowing to reuse it in the same context.
func (*Cursor[T]) TotalItems ¶
TotalItems returns the total number of items, or -1 if unknown.
func (*Cursor[T]) TotalPages ¶
TotalPages returns the total number of pages, or -1 if unknown.
type Pagination ¶
type Pagination struct {
First string `json:"first,omitempty"`
Prev string `json:"prev,omitempty"`
Last string `json:"last,omitempty"`
Next string `json:"next,omitempty"`
}
Pagination contains all cursors to navigate from a cursor.
type Pointer ¶
type Pointer interface {
// Args returns the arguments to use in a statement.
Args() []any
// IsZero returns true if the pointer is a zero value.
IsZero() bool
}
Pointer must be implemented by any cursor point.
type Statement ¶
type Statement[T Pointer] struct { // Cursor is the cursor of pagination. Cursor *Cursor[T] // DescendingOrder defines the result's order by default. DescendingOrder bool }
Statement allows building of SQL query for MySQL or MariaDB. The idea is to build a SQL statement like this one to going forward and returns results based on the cursor with descending order.
WITH d AS (
SELECT * FROM table t WHERE cursor > ? ORDER BY cursor ASC LIMIT ?
) SELECT * FROM p ORDER BY cursor DESC;
Limit statement adds one to the cursor's limit in order to know the start of the next cursor and if there is more data.
func (Statement[T]) Limit ¶
Limit returns the row count to restrict the number of returned rows. The value is incremented by one to check if there is more to fetch.