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
go get -u github.com/dmitryburov/gorm-paginator
- GORM Guides: https://gorm.io
- See full example: example/example.go
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