Logo

dev-resources.site

for different kinds of informations.

Pagination using Gorm scopes

Published at
6/24/2021
Categories
go
gorm
orm
Author
rafaelgfirmino
Categories
3 categories in total
go
open
gorm
open
orm
open
Author
14 person written this
rafaelgfirmino
open
Pagination using Gorm scopes

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 
Enter fullscreen mode Exit fullscreen mode

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   
}   
Enter fullscreen mode Exit fullscreen mode


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())   
    }   
}   
Enter fullscreen mode Exit fullscreen mode


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 
}   
Enter fullscreen mode Exit fullscreen mode

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"   
    }   
  ] 
}   
Enter fullscreen mode Exit fullscreen mode
gorm Article's
26 articles in total
Favicon
Gorm: Sneak Peek of Custom Data Types
Favicon
GORM, PostgreSQL & Atlas
Favicon
Gorm Pagination With Ease
Favicon
How to Use Arrays as Parameters in Golang?
Favicon
Basic CRUD Operations Using Golang, Gin Gonic, and GORM
Favicon
GORM and Goose Migrations
Favicon
Gin + Gorm Practical Guide, Implementing a Simple Q&A Community Backend Service in One Hour
Favicon
A secret weapon to improve the efficiency of golang development, a community backend service was developed in one day
Favicon
Building CRUD Operations in Golang 🎉
Favicon
Some GORM Tips and Notes
Favicon
Gorm level UP: how to upgrade and start to use Gorm v2
Favicon
5chan API - Golang, GORM, Go-fiber
Favicon
Pagination using Gorm scopes
Favicon
GOlang URL shortener service using postgres, redis, bulma
Favicon
RestFull API Detailing
Favicon
Daily Blog
Favicon
Rest API Database Blog Post
Favicon
Prevent updating query updates all records in GORM
Favicon
Init project
Favicon
Creating a complete golang rest api from zero to Hero
Favicon
ตัวอย่างการสร้าง Many to Many Association ผ่าน GORM
Favicon
DataBinding and GORM objects
Favicon
Creating an opinionated GraphQL server with Go - Part 4
Favicon
Creating an opinionated GraphQL server with Go - Part 3
Favicon
Grails: Mocking domain objects/criteria that reference Joda Time objects
Favicon
Go's ORM framework GORM best practices indefinite parameter usage

Featured ones: