akmamun / gorm-pagination

Gorm Pagination Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gorm-pagination

Gorm Pagination library

Installation

go get github.com/akmamun/gorm-pagination

Usage Example

  • 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"`
    }
	
    var example Example
    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"`
    }
    
	var example Example
    query := db.Model(&example).Where("id = ?", 1)
    data, err := pagination.Paginate[Example](query, limit, Offset)

Pagination View

  • Input Params limit and offset
  • 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
	}
	
	var example Example
	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)
}

Credit https://github.com/hellokaton/gorm-paginator

About

Gorm Pagination Library

License:MIT License


Languages

Language:Go 100.0%