pagination

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2022 License: MIT Imports: 2 Imported by: 0

README

gorm-pagination

Gorm Pagination library

Installation

go get github.com/akmamun/gorm-pagination/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
     
    pagination.Paginate(&pagination.Param{
    DB:      *gorm.DB,
    Limit:   limit,
    Offset:  offset,
    OrderBy: "id ASC",
    }, &example)
  • 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

    data := pagination.Paginate(&pagination.Param{
            DB:      *gorm.DB,
            Limit:   limit,
            Offset:  offset,
            OrderBy: "id ASC",
            Query : Example{Id:1}
    }, &example)
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/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
	}
	ex := pagination.Paginate(&pagination.Param{
		DB:      db,
		Limit:   10,
		Offset:  1,
		OrderBy: "id ASC",
	}, &example)

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(&ex)
}
Credit https://github.com/hellokaton/gorm-paginator

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Param

type Param struct {
	DB      *gorm.DB
	Offset  int64
	Limit   int64
	OrderBy string
	Query   interface{}
}

type Result

type Result struct {
	TotalRecord int64       `json:"total_record"`
	TotalPage   int64       `json:"total_page"`
	Offset      int64       `json:"offset"`
	Limit       int64       `json:"limit"`
	PrevPage    int64       `json:"prev_page"`
	NextPage    int64       `json:"next_page"`
	Results     interface{} `json:"results"`
}

func Paginate

func Paginate(param *Param, resultData interface{}) *Result

Directories

Path Synopsis
pagination module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL