Skip to content

dmitryburov/gorm-paginator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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