paginator

package module
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 3 Imported by: 0

README

GORM V2 paginator

A paginator doing cursor-based pagination based on GORM v2.

Please, checkout release and docs, for introduces some incompatible-API change and many improvements:
GORM 2.0 Release Note


Installation

go get -u github.com/dmitryburov/gorm-paginator

Usage

type Book struct {
	gorm.Model
	Title string
}

var (
    dbEntity = db
    paging   = paginator.Paging{}
    bookList = struct {
        Items      []*Book
        Pagination *paginator.Pagination
    }{}
)

// change paging params from query data
// if len(query.Get("limit")) > 0 && query.Get("limit") != "" {
//     paging.Limit, _ = strconv.Atoi(query.Get("limit"))
// }
 
// change DB filters and ect.
// dbEntity = dbEntity.Where("id > ?", 1)

bookList.Pagination, err := paginator.Pages(&paginator.Param{
    DB:     dbEntity,
    Paging: &paging,
}, &bookList.Items)
if err != nil {
    log.Fatal("Error get list: ", err.Error())
}

// if empty list
//if bookList.Pagination.IsEmpty() {
//
//}

fmt.Printf("%+v\n", bookList.Items)      // result data
fmt.Printf("%+v\n", booklist.Pagination) // result pagination


License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pagination

type Pagination struct {
	TotalRecords int64 `json:"totalRecords"`
	TotalPage    int   `json:"totalPage"`
	Offset       int   `json:"offset"`
	Limit        int   `json:"limit"`
	Page         int   `json:"page"`
	PrevPage     int   `json:"prevPage"`
	NextPage     int   `json:"nextPage"`
}

func Pages

func Pages(p *Param, result interface{}) (paginator *Pagination, err error)

Endpoint for pagination

func (Pagination) IsEmpty added in v0.9.5

func (p Pagination) IsEmpty() bool

type Paging

type Paging struct {
	Page    int      `json:"page"`
	OrderBy []string `json:"orderBy"`
	Limit   int      `json:"limit"`
	ShowSQL bool
}

type Param

type Param struct {
	DB     *gorm.DB
	Paging *Paging
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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