gopgpager

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

gopgpager

go-pg (https://github.com/go-pg) cursor pagination

Installation

$ go get github.com/SramPay/gopgpager

Usage

First, import the package:

import "github.com/SeamPay/gopgpager"

Now, you can access the package through gopgpager namespace.

  • CursorPaginator: uses a database condition Where("(id, created_at) <= (?, ?)", id, createdAt).

It works in four steps:

  • Create a store (go-pg)
  • Create a CursorPaginator instance with: your store, and the request
req := pagination.Request{
    PageToken: pageToken, // string
	PageSize:  pageSize, // int32
}

// pageToken is a base64 encoded comma separated string made up the id and createdAt
// pageSize the amount of records to return
  • Call the paginator.Page() method to process the pagination
  • Call the paginator.Next() method to get the next paginator instance

Example:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE IF NOT EXISTS users
(
    id                  uuid      NOT NULL DEFAULT uuid_generate_v4()
        CONSTRAINT users_pkey
            PRIMARY KEY,
    created_at          TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at          TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    first_name          VARCHAR   NOT NULL,
    last_name           VARCHAR   NOT NULL,
);

-- the order of columns is important. If you reverse it, the index will not be useful. 
CREATE INDEX created_at_id_idx ON customers (created_at, id);
// create a slice for your go-pg entity.
var users []entity.User

// create the store. It takes your database connection pointer,
// and a pointer to entity.
store, err := gopgpager.NewGOPGStore(&db, &users)
if err != nil {
	log.Fatal(err)
}

// create the query request
req := pagination.Request{
	PageToken: "", // first request has no token
	PageSize:  1,
}

// create a paginator instance and pass your store, your request
paginator, err := gopgpager.NewCursorPaginator(store, req)
if err != nil {
	log.Fatal(err)
}

// call the paginator.Page() method to get the page instance.
err = paginator.Page()
if err != nil {
	return nil, "", err
}

// paginator instance contains everything you need.
paginator.Limit
paginator.NextToken.String // set to pageToken on the request

// get the next page.
nextPaginator, err := paginator.Next()
if err != nil {
	log.Fatal(err)
}

Contributing

Documentation

Index

Constants

View Source
const (
	DefaultFilterColumnKey   = ""
	DefaultFilterColumnValue = ""
)
View Source
const (
	// DefaultLimit is the default number of items per page.
	DefaultLimit = 20
)

Variables

This section is empty.

Functions

func GetCursorFromRequest

func GetCursorFromRequest(request Request) string

GetCursorFromRequest returns current cursor.

func GetLimitFromRequest

func GetLimitFromRequest(request Request) int32

GetLimitFromRequest returns current limit.

Types

type CursorPaginator

type CursorPaginator struct {
	Cursor      interface{} `json:"-"`
	PreviousURI null.String `json:"-"`
	// contains filtered or unexported fields
}

CursorPaginator is the paginator with cursor pagination system.

func NewCursorPaginator

func NewCursorPaginator(store Store, request Request, options *Options) (*CursorPaginator, error)

NewCursorPaginator returns a new CursorPaginator instance.

func (*CursorPaginator) HasNext

func (c *CursorPaginator) HasNext() bool

HasNext returns true if has next page.

func (*CursorPaginator) MakeNextToken

func (p *CursorPaginator) MakeNextToken() null.String

MakeNextToken returns the next page Token.

func (*CursorPaginator) Next

func (p *CursorPaginator) Next() (Paginator, error)

Next returns next items

func (*CursorPaginator) Page

func (p *CursorPaginator) Page() error

Page searches and returns the items

type GOPGStore

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

GORMStore is the store for GOPG ORM.

func NewGOPGStore

func NewGOPGStore(db *pg.DB, items interface{}) (*GOPGStore, error)

NewGOPGStore returns a new go-pg store instance.

func (*GOPGStore) GetItems

func (s *GOPGStore) GetItems() interface{}

GetItems return the current result

func (*GOPGStore) PaginateCursor

func (s *GOPGStore) PaginateCursor(limit int32, cursor interface{}, filterKey, filterValue string, filter bool, hasNext *bool) error

type Options added in v0.1.2

type Options struct {
	// FilterColumnKey is the name of column that is used for extra WHERE clause
	FilterColumnKey string
	// FilterColumnValue is the value of column that is used for extra WHERE clause
	FilterColumnValue string
	// Filter turn true to apply extra WHERE clause
	Filter bool
}

Options are paginator options

func NewOptions added in v0.1.2

func NewOptions() *Options

NewOptions returns defaults options

type Paginator

type Paginator interface {
	Page() error
	Next() (Paginator, error)
	HasNext() bool
	MakeNextToken() null.String
}

Paginator is a paginator interface.

type Request

type Request struct {
	PageToken string
	PageSize  int32
}

type Store

type Store interface {
	PaginateCursor(limit int32, cursor interface{}, filterKey, filterValue string, filter bool, hasNext *bool) error
	GetItems() interface{}
}

Store is a store.

Jump to

Keyboard shortcuts

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