paging

一组用于在 Go 中对数据进行分页的实用程序。「A small set of utilities to paginate your data in Go」

  • 所有者: ulule/paging
  • 平台: Linux, Mac, Windows
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

paging

A small set of utilities to paginate your data in Go.

Installation

$ go get github.com/ulule/paging

Usage

First, import the package:

import "github.com/ulule/paging"

Now, you can access the package through paging namespace.

There are two types of paginators:

  • OffsetPaginator: uses a database offset. Returns the total of elements.
  • CursorPaginator: uses a database condition like ID > ? or creation_date < ?. Does not return the total number of items but increases performances.

It works in four steps:

  • Create a store (which is basically where your entities are stored)
  • Create paginator options (or use default ones)
  • Create an OffsetPaginator or a CursorPaginator instance with: your store, the HTTP request, and options
  • Call the paginator.Page() method to process the pagination
  • Call the paginator.Previous() method to get the previous paginator instance. (Previous page isn't available for cursor pagination system)
  • Call the paginator.Next() method to get the next paginator instance

Example with OffsetPaginator and GORM:

// Before anything... create a slice for your GORM models.
// Let's assume we have 100 users in our database.
users := []User{}

// Step 1: create the store. It takes your database connection pointer, a
// pointer to models and the GORM "ORDER BY" string.
store, err := paging.NewGORMStore(&db, &users)
if err != nil {
        log.Fatal(err)
}

// Step 2: create options. Here, we use the default ones (see below).
options := paging.NewOptions()

// Step 3: create a paginator instance and pass your store, your current HTTP
// request and your options as arguments.
request, _ := http.NewRequest("GET", "http://example.com?limit=20&offset=0", nil)
paginator := paging.NewOffsetPaginator(store, request, options)

// Step 4: call the paginator.Page() method to get the page instance.
err := paginator.Page()
if err != nil {
        log.Fatal(err)
}

// Your paginator instance contains everything you need.
assert.True(int64(20), paginator.Limit)
assert.True(int64(0), paginator.Offset)
assert.True(int64(100), paginator.Count)
assert.False(paginator.PreviousURI.Valid) // It's a null string because no previous page
assert.True(paginator.NextURI.Valid)
assert.Equal( "?limit=20&offset=20", paginator.NextURI.String)

// And our "users" slice is now populated with 20 users ordered by name.
assert.Equal(20, len(users))

// Now get the next page.
nextPaginator, err := paginator.Next()
if err != nil {
        log.Fatal(err)
}

// Or the previous page.
previousPaginator, err := paginator.Previous()
if err != nil {
        log.Fatal(err)
}

Paginator options are:

  • DefaultLimit (int64): the number of items per page (defaults to 20)
  • MaxLimit (int64): the maximum limit that can be set (defaults to 20)
  • LimitKeyName (string): the query string key name for limit (defaults to limit)
  • OffsetKeyName (string): the query string key name for offset (defaults to offset)
  • CursorOptions.Mode (string): set type of cursor, an idCursor or a dateCursor (time.Time) (defaults to idCursor)
  • CursorOptions.KeyName (string): the query string key name for the cursor (defaults to since)
  • CursorOptions.DBName (string): the cursor's database column name (defaults to id)
  • CursorOptions.StructName (string): the cursor struct field name (defaults to ID)
  • CursorOptions.Reverse (bool): if true, order is reversed (DESC) (defaults to false)

Contributing

主要指标

概览
名称与所有者ulule/paging
主编程语言Go
编程语言Makefile (语言数: 2)
平台Linux, Mac, Windows
许可证MIT License
所有者活动
创建于2015-10-19 08:06:03
推送于2021-05-10 06:24:32
最后一次提交2019-01-24 15:08:47
发布数3
最新版本名称v0.3.0 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数140
关注者数10
派生数21
提交数48
已启用问题?
问题数2
打开的问题数0
拉请求数8
打开的拉请求数0
关闭的拉请求数5
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?