dev-resources.site
for different kinds of informations.
Pagination using Gorm scopes
Published at
6/24/2021
Categories
go
gorm
orm
Author
rafaelgfirmino
Author
14 person written this
rafaelgfirmino
open
Gorm is a fantastic ORM writen in Go for gophers.
Today I will show you how to create pagination using Gorm Scopes.
Scopes allow you to re-use commonly used logic, the shared logic needs to be defined as type
func(*gorm.DB) *gorm.DB
The first step is create a Pagination struct.
//pkg.pagination
package pkg
type Pagination struct {
Limit int `json:"limit,omitempty;query:limit"`
Page int `json:"page,omitempty;query:page"`
Sort string `json:"sort,omitempty;query:sort"`
TotalRows int64 `json:"total_rows"`
TotalPages int `json:"total_pages"`
Rows interface{} `json:"rows"`
}
func (p *Pagination) GetOffset() int {
return (p.GetPage() - 1) * p.GetLimit()
}
func (p *Pagination) GetLimit() int {
if p.Limit == 0 {
p.Limit = 10
}
return p.Limit
}
func (p *Pagination) GetPage() int {
if p.Page == 0 {
p.Page = 1
}
return p.Page
}
func (p *Pagination) GetSort() string {
if p.Sort == "" {
p.Sort = "Id desc"
}
return p.Sort
}
Second step is create a Gorm Scope.
func paginate(value interface{}, pagination *pkg.Pagination, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
var totalRows int64
db.Model(value).Count(&totalRows)
pagination.TotalRows = totalRows
totalPages := int(math.Ceil(float64(totalRows) / float64(pagination.Limit)))
pagination.TotalPages = totalPages
return func(db *gorm.DB) *gorm.DB {
return db.Offset(pagination.GetOffset()).Limit(pagination.GetLimit()).Order(pagination.GetSort())
}
}
The third step is use Gorm scope in your repository.
type CategoryGorm struct {
db *gorm.DB
}
func (cg *CategoryGorm) List(pagination pkg.Pagination) (*pkg.Pagination, error) {
var categories []*Category
cg.db.Scopes(paginate(categories,&pagination, cg.db)).Find(&categories)
pagination.Rows = categories
return &pagination, nil
}
Rest Example
I won't talk about how you could implement your handles or access the repository, maybe another time.
The most important thing you need is to return the pkg.pagination for you client.
In a rest application, if I send
http://localhost:3000/categories?limit=10&page=5
Then the output would be:
{
"limit": 10,
"page": 5,
"sort": "Id desc",
"total_rows": 41,
"total_pages": 5,
"rows": [
{
"id": "0acd5600-51b9-42e9-9fd0-ff422a6de1d1",
"category": "example41"
}
]
}
gorm Article's
26 articles in total
Gorm: Sneak Peek of Custom Data Types
read article
GORM, PostgreSQL & Atlas
read article
Gorm Pagination With Ease
read article
How to Use Arrays as Parameters in Golang?
read article
Basic CRUD Operations Using Golang, Gin Gonic, and GORM
read article
GORM and Goose Migrations
read article
Gin + Gorm Practical Guide, Implementing a Simple Q&A Community Backend Service in One Hour
read article
A secret weapon to improve the efficiency of golang development, a community backend service was developed in one day
read article
Building CRUD Operations in Golang 🎉
read article
Some GORM Tips and Notes
read article
Gorm level UP: how to upgrade and start to use Gorm v2
read article
5chan API - Golang, GORM, Go-fiber
read article
Pagination using Gorm scopes
currently reading
GOlang URL shortener service using postgres, redis, bulma
read article
RestFull API Detailing
read article
Daily Blog
read article
Rest API Database Blog Post
read article
Prevent updating query updates all records in GORM
read article
Init project
read article
Creating a complete golang rest api from zero to Hero
read article
ตัวอย่างการสร้าง Many to Many Association ผ่าน GORM
read article
DataBinding and GORM objects
read article
Creating an opinionated GraphQL server with Go - Part 4
read article
Creating an opinionated GraphQL server with Go - Part 3
read article
Grails: Mocking domain objects/criteria that reference Joda Time objects
read article
Go's ORM framework GORM best practices indefinite parameter usage
read article
Featured ones: