Answers for "pagination gorm"

SQL
1

gorm pagination

func Paginate(r *http.Request) func(db *gorm.DB) *gorm.DB { 
	return func (db *gorm.DB) *gorm.DB {    
    page, _ := strconv.Atoi(r.Query("page"))    
    if page == 0 {      
    	page = 1    
    }    
    pageSize, _ := strconv.Atoi(r.Query("page_size"))    
    switch {    
    	case pageSize > 100:      
        	pageSize = 100    
        case pageSize <= 0:      
        	pageSize = 10    
     }    
     offset := (page - 1) * pageSize    
     return db.Offset(offset).Limit(pageSize)  
	 }
}

db.Scopes(Paginate(r)).Find(&users)db.Scopes(Paginate(r)).Find(&articles)
Posted by: Guest on May-22-2021

Code answers related to "SQL"

Browse Popular Code Answers by Language