Gorm Pagination library
go get github.com/akmamun/gorm-pagination
- Example 1
type Example struct {
Id int `json:"id"`
Data string `json:"data" binding:"required"`
CreatedAt *time.Time `json:"created_at,string,omitempty"`
UpdatedAt *time.Time `json:"updated_at,string,omitempty"`
}
query := db.Model(&example)
data, err := pagination.Paginate[Example](*query, limit, Offset)
- Example 2
type Example struct {
Id int `json:"id"`
Data string `json:"data" binding:"required"`
CreatedAt *time.Time `json:"created_at,string,omitempty"`
UpdatedAt *time.Time `json:"updated_at,string,omitempty"`
}
query := db.Model(&example).Where("id = ?", 1)
data, err := pagination.Paginate[Example](*query, limit, Offset)
- Input Params
limit
andoffset
- Example Url
localhost:8000/test?limit=1&offset=1
- Output Format
{
"total_record": 17,
"total_page": 17,
"offset": 1,
"limit": 1,
"prev_page": 1,
"next_page": 2,
"results": [
{
"id": 2,
"data": "this is test data",
"created_at": "2022-09-13T17:42:36.358116Z",
"updated_at": "2022-09-13T17:42:36.358116Z"
}
]
}
- Full Example
package main
import (
"encoding/json"
"fmt"
"github.com/akmamun/gorm-pagination"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Example struct {
Id int `json:"id"`
Data string `json:"data" binding:"required"`
}
func main() {
var example Example
insertedData := Example{Data: "data"}
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
if err == nil {
db.AutoMigrate(&example)
db.Create(&insertedData)
fmt.Println("Inserted!")
} else {
fmt.Println(err)
return
}
query := db.Model(&example).Where("id = ?", 1)
data, err := pagination.Paginate[Example](*query, limit, Offset)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&data)
}