paginator

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2019 License: MIT Imports: 7 Imported by: 4

README

gorm-cursor-paginator Build Status Coverage Status Go Report Card Codacy Badge

A paginator doing cursor-based pagination based on GORM

Installation

go get -u github.com/pilagod/gorm-cursor-paginator

Usage by Example

Assume there is an query struct for paging:

type PagingQuery struct {
    Cursor paginator.Cursor
    Limit  *int
    Order  *string
}

and a GORM model:

type Model struct {
    ID          int
    CreatedAt   time.Time
}

You can simply build up a new cursor paginator from the PagingQuery like:

import (
    paginator "github.com/pilagod/gorm-cursor-paginator"
)

func GetModelPaginator(q PagingQuery) *paginator.Paginator {
    p := paginator.New()

    p.SetKeys("CreatedAt", "ID") // [defualt: "ID"] (supports multiple keys, and order of keys matters)

    if q.Cursor.After != nil {
        p.SetAfterCursor(*q.Cursor.After) // [default: nil]
    }

    if q.Cursor.Before != nil {
        p.SetBeforeCursor(*q.Cursor.Before) // [default: nil]
    }

    if q.Limit != nil {
        p.SetLimit(*q.Limit) // [default: 10]
    }

    if q.Order != nil && *q.Order == "asc" {
        p.SetOrder(paginator.ASC) // [default: paginator.DESC]
    }
    return p
}

Then you can start to do pagination easily with GORM:

func Find(db *gorm.DB, q PagingQuery) ([]Model, paginator.Cursor, error) {
    var models []Model

    stmt := db.Where(/* ... other filters ... */)
    stmt = db.Or(/* ... more other filters ... */)

    // get paginator for Model
    p := GetModelPaginator(q)

    // use GORM-like syntax to do pagination
    result := p.Paginate(stmt, &models)

    if result.Error != nil {
        // ...
    }
    // get cursor for next iteration
    cursor := p.GetNextCursor()

    return models, cursor, nil
}

After paginating, you can call GetNextCursor(), which returns a Cursor struct containing cursor for next iteration:

type Cursor struct {
    After  *string `json:"after"`
    Before *string `json:"before"`
}

That's all ! Enjoy your paging in the GORM world 🎉

License

© Chun-Yan Ho (pilagod), 2018-NOW

Released under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor added in v1.0.0

type Cursor struct {
	After  *string `json:"after"`
	Before *string `json:"before"`
}

Cursor cursor data

type Order added in v1.0.1

type Order string

Order type for order

const (
	ASC  Order = "ASC"
	DESC Order = "DESC"
)

Orders

type Paginator

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

Paginator a builder doing pagination

func New

func New() *Paginator

New inits paginator

func (*Paginator) GetNextCursor added in v1.0.0

func (p *Paginator) GetNextCursor() Cursor

GetNextCursor returns cursor for next pagination

func (*Paginator) Paginate

func (p *Paginator) Paginate(stmt *gorm.DB, out interface{}) *gorm.DB

Paginate paginates data

func (*Paginator) SetAfterCursor

func (p *Paginator) SetAfterCursor(afterCursor string)

SetAfterCursor sets paging after cursor

func (*Paginator) SetBeforeCursor

func (p *Paginator) SetBeforeCursor(beforeCursor string)

SetBeforeCursor sets paging before cursor

func (*Paginator) SetKeys

func (p *Paginator) SetKeys(keys ...string)

SetKeys sets paging keys

func (*Paginator) SetLimit

func (p *Paginator) SetLimit(limit int)

SetLimit sets paging limit

func (*Paginator) SetOrder

func (p *Paginator) SetOrder(order Order)

SetOrder sets paging order

Jump to

Keyboard shortcuts

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