pagination

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2020 License: MIT Imports: 2 Imported by: 0

README

gorm-pagination

Usage

go get github.com/Hironaga06/gorm-pagination
type User struct {
	gorm.Model
	Name string
}

var users []User
db = db.Where("id > ?", 0)

var users []User
p := pagination.New(db, 1, 3, []string{"id asc"}, &users, true)
result, err := p.Paging()
if err != nil {
	panic(err)
}
fmt.Println(result)

With Gin

r := gin.Default()
r.GET("/", func(c *gin.Context) {
	offset, err := strconv.Atoi(c.DefaultQuery("offset", "1"))
	if err != nil {
		panic(err)
	}
	limit, err := strconv.Atoi(c.DefaultQuery("limit", "3"))
	if err != nil {
		panic(err)
	}
	var users []User
	p := pagination.New(db, offset, limit, []string{"id asc"}, &users, true)
	result, err := p.Paging()
	if err != nil {
		panic(err)
	}
	c.JSON(200, result)
})
r.Run()

License

MIT

Documentation

Index

Constants

View Source
const (
	DefaultOffset = 1
	DefaultLimit  = 10
)

Variables

This section is empty.

Functions

func New

func New(db *gorm.DB, offset, limit int, order []string, models interface{}, debug bool) *pagination

Types

type Result

type Result struct {
	TotalRecord int64       `json:"totalRecord"`
	TotalPage   int         `json:"totalPage"`
	Records     interface{} `json:"records"`
	Offset      int         `json:"offset"`
	Limit       int         `json:"limit"`
	CurrentPage int         `json:"currentPage"`
	PrevPage    int         `json:"prevPage"`
	NextPage    int         `json:"nextPage"`
}

Jump to

Keyboard shortcuts

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