Giter Site home page Giter Site logo

pratikmallya / gorm-cursor-paginator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pilagod/gorm-cursor-paginator

0.0 1.0 0.0 34 KB

A paginator doing cursor-based pagination based on GORM

Home Page: https://github.com/pilagod/gorm-cursor-paginator

License: MIT License

Go 100.00%

gorm-cursor-paginator's Introduction

gorm-cursor-paginator Build Status Coverage Status Go Report Card Codacy Badge

A paginator doing cursor-based pagination based on GORM

Installation

go get -u github.com/pilagod/gorm-cursor-paginator

Usage by Example

Assume there is an query struct for paging:

type PagingQuery struct {
    Cursor paginator.Cursor
    Limit  *int
    Order  *string
}

and a GORM model:

type Model struct {
    ID          int
    CreatedAt   time.Time
}

You can simply build up a new cursor paginator from the PagingQuery like:

import (
    paginator "github.com/pilagod/gorm-cursor-paginator"
)

func GetModelPaginator(q PagingQuery) *paginator.Paginator {
    p := paginator.New()

    p.SetKeys("CreatedAt", "ID") // [default: "ID"] (supports multiple keys, and order of keys matters)

    if q.Cursor.After != nil {
        p.SetAfterCursor(*q.Cursor.After) // [default: nil]
    }

    if q.Cursor.Before != nil {
        p.SetBeforeCursor(*q.Cursor.Before) // [default: nil]
    }

    if q.Limit != nil {
        p.SetLimit(*q.Limit) // [default: 10]
    }

    if q.Order != nil && *q.Order == "asc" {
        p.SetOrder(paginator.ASC) // [default: paginator.DESC]
    }
    return p
}

Then you can start to do pagination easily with GORM:

func Find(db *gorm.DB, q PagingQuery) ([]Model, paginator.Cursor, error) {
    var models []Model

    stmt := db.Where(/* ... other filters ... */)
    stmt = db.Or(/* ... more other filters ... */)

    // get paginator for Model
    p := GetModelPaginator(q)

    // use GORM-like syntax to do pagination
    result := p.Paginate(stmt, &models)

    if result.Error != nil {
        // ...
    }
    // get cursor for next iteration
    cursor := p.GetNextCursor()

    return models, cursor, nil
}

After paginating, you can call GetNextCursor(), which returns a Cursor struct containing cursor for next iteration:

type Cursor struct {
    After  *string `json:"after"`
    Before *string `json:"before"`
}

That's all ! Enjoy your paging in the GORM world ๐ŸŽ‰

License

ยฉ Chun-Yan Ho (pilagod), 2018-NOW

Released under the MIT License

gorm-cursor-paginator's People

Contributors

pilagod avatar pratikmallya avatar codacy-badger avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.