paginator

package
v0.0.0-...-37076da Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package paginator is a helper package for paginating results.

It's made for easy paginate with https://gorm.io

Example
package main

import (
	"fmt"

	"github.com/jeanmolossi/vigilant-waddle/src/pkg/paginator"
)

func main() {
	p := paginator.New(
		paginator.WithBaseURL("https://example.com/article/%s", "article-slug"),
		paginator.WithPage(2),
		paginator.WithItemsPerPage(15),
	)

	fmt.Println(p.Page(), p.ShouldPaginate(), p.GetItemsPerPage())

}
Output:

2 true 15

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(p *paginator) error

Option is a function that can be passed to NewPaginator to configure the paginator.

func WithBaseURL

func WithBaseURL(format string, args ...interface{}) Option

func WithItemsPerPage

func WithItemsPerPage(itemsPerPage int) Option

func WithPage

func WithPage(page uint16) Option

type Paginator

type Paginator interface {
	// Page returns the current page.
	Page() uint16
	// ShouldPaginate returns true if pagination is needed.
	ShouldPaginate() bool
	// GetItemsPerPage returns the number of items per page.
	GetItemsPerPage() int
	// Paginate is a gorm pagination scope implementation.
	// It returns a gorm.DB with pagination applied.
	//
	// Example:
	//
	//      pagination := paginator.NewPaginator(
	//          paginator.WithPage(2),
	//      )
	//
	//      if pagination.ShouldPaginate() {
	//          gormDB = gormDB.Scopes(pagination.Paginate)
	//      }
	Paginate(db *gorm.DB) *gorm.DB
	// contains filtered or unexported methods
}

Paginator is the interface that wraps the basic Paginate methods.

func New

func New(opts ...Option) Paginator

NewPaginator creates a new paginator. It accepts a variadic list of options to configure the paginator.

Jump to

Keyboard shortcuts

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