All Projects → raphaelvigee → go-paginate

raphaelvigee / go-paginate

Licence: Apache-2.0 license
Cursor-based go paginator

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to go-paginate

gorm-cursor-paginator
A paginator doing cursor-based pagination based on GORM
Stars: ✭ 92 (+91.67%)
Mutual labels:  pagination, gorm
Mongoose Paginate V2
A cursor based custom pagination library for Mongoose with customizable labels.
Stars: ✭ 283 (+489.58%)
Mutual labels:  pagination, cursor
mongoose-graphql-pagination
GraphQL cursor pagination (Relay-like) for Mongoose models.
Stars: ✭ 29 (-39.58%)
Mutual labels:  pagination, cursor
Gorm Paginator
gorm pagination extension
Stars: ✭ 136 (+183.33%)
Mutual labels:  pagination, gorm
mongoose-aggregate-paginate-v2
A cursor based custom aggregate pagination library for Mongoose with customizable labels.
Stars: ✭ 103 (+114.58%)
Mutual labels:  pagination, cursor
gorm-paginator
gorm pagination extension
Stars: ✭ 154 (+220.83%)
Mutual labels:  pagination, gorm
pink-lady
a template project of gin app.
Stars: ✭ 44 (-8.33%)
Mutual labels:  gorm
vue-pagination
🔵一个`bootstrap`风格的`vue.js`(2.0)分页组件
Stars: ✭ 28 (-41.67%)
Mutual labels:  pagination
iris-gorm-demo
iris+gorm+mysql的restful api项目起手式
Stars: ✭ 92 (+91.67%)
Mutual labels:  gorm
ember-cli-blog
Tom Dale's blog example updated for the Ember CLI
Stars: ✭ 87 (+81.25%)
Mutual labels:  pagination
developer-guides
Developer Guides
Stars: ✭ 25 (-47.92%)
Mutual labels:  pagination
pagination-pager
Ember.js Component for Bootstrap 3 pagination & pager components
Stars: ✭ 56 (+16.67%)
Mutual labels:  pagination
RxPagingLoading
Easy handling of the Paging or Loading screens states
Stars: ✭ 45 (-6.25%)
Mutual labels:  pagination
gothic
🦇 Gothic is a user registration and authentication SWT/JWT microservice. It supports REST, gRPC, and gRPC Web API, reCAPTCHA & a variety of DBs with Gorm.
Stars: ✭ 65 (+35.42%)
Mutual labels:  gorm
FutureBuilderWithPagination
we're gonna look out how to work with Future Builder and show the result in GridView. We'll also see how to use Pagination with Future Builder.Future Builder is a widget that returns another widget based on futures execution result. It builds itself based on the latest AsyncSnapshots.
Stars: ✭ 45 (-6.25%)
Mutual labels:  pagination
Pagination-and-Search-Laravel
Example of pagination and search functionality combined in Laravel Framework
Stars: ✭ 19 (-60.42%)
Mutual labels:  pagination
pagination
Aplus Framework Pagination Library
Stars: ✭ 167 (+247.92%)
Mutual labels:  pagination
gorm-neo4j
GORM for Neo4j
Stars: ✭ 16 (-66.67%)
Mutual labels:  gorm
go-tenancy
快速实现 SaaS 多租户平台项目
Stars: ✭ 157 (+227.08%)
Mutual labels:  gorm
vue3-table-lite
A simple and lightweight data table component for Vue.js 3. Features sorting, paging, row check, dynamic data rendering, supported TypeScript, and more.
Stars: ✭ 148 (+208.33%)
Mutual labels:  pagination

go-paginate

PkgGoDev Test

An efficient go data cursor-based paginator.

  • Plug and play
  • Easy to use
  • Fully customizable
go get github.com/raphaelvigee/go-paginate

Why?

A lot of articles on the internet summarize very well the benefits of cursor-based pagination, but here are the highlights:

  • It scales: Unlike OFFSET/LIMIT-based pagination doesn't scale well for large datasets
  • Suitable for real-time data: having a fixed point in the flow of data prevents duplicates/missing entries

It does have an issue, it is hard to implement, that's why go-paginate exists :)

Drivers

  • gorm:

    • Supports multiple columns with different orderings directions (ex: ORDER BY id ASC, name DESC)
  • Implement your own: See driver.Driver and base.Driver

Can't find what you are looking for? Open an issue!

Usage

With gorm

Errors omitted for brevity

Create the paginator, defining the criteria (columns and ordering):

pg := paginator.New(paginator.Options{
    Driver: gorm.New(gorm.Options{
        Columns: []gorm.Column{
            {
                Name: "created_at",
            },
        },
    }),
})

Create the cursor instance, most likely from the request (in the initial request, the cursor is an empty string):

c, err := pg.Cursor("<cursor from client>", cursor.After, 2)

Create a transaction with appropriate filtering etc and request the pagination info:

tx := db.Model(&User{}).Where(...)
page, err := pg.Paginate(c, tx)

// That should be sent back to the client along with the data
fmt.Println(page.PageInfo.HasPreviousPage)
fmt.Println(page.PageInfo.HasNextPage)
fmt.Println(page.PageInfo.StartCursor)
fmt.Println(page.PageInfo.EndCursor)

Retrieve the underlying data for the page:

var users []User
err := page.Query(&users)

A full working example can be found in _examples/gorm.

Custom cursor

By default, the cursor will be marshalled through msgpack for size concerns, and base64 for portability. One can choose to do differently (for example encrypting them...), see the implementation of cursor.MsgPack and cursor.Base64.

pg := paginator.New(paginator.Options{
    ...
    CursorMarshaller: cursor.Chain(cursor.MsgPack(), cursor.Base64(base64.StdEncoding))
})

Release

TAG=v0.0.1 make tag
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].