mongopagination

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2020 License: MIT Imports: 5 Imported by: 0

README

Golang Mongo Pagination For Package mongo-go-driver

Build Go Report Card GoDoc

Simple and easy to use Pagination with information like Total, Page, PerPage, Prev, Next and TotalPage.

Install

$ go get -u -v github.com/roshanr83/mongo-go-pagination

or with dep

$ dep ensure -add github.com/roshanr83/mongo-go-pagination

Demo


    filter := bson.M{}
	var limit int64 = 10
	var page int64 = 1
	paging := PagingQuery{
		collection: db.Collection(DatabaseCollectionName),
		filter: filter,
		limit: limit,
		page: page,
		sortField: "createdAt",
		sortValue: -1,
	}
	paginatedData, err := paging.Find()
	
	// paginated data is in paginatedData.Data
	// pagination info can be accessed in  paginatedData.Pagination
	// if you want to marshal data to your defined struct
	
	var lists []TodoTest
    for _, raw := range paginatedData.Data {
        var todo TodoTest
        if err := bson.Unmarshal(raw, &todo); err == nil {
            lists = append(lists, todo)
        }
    }


Running the tests

$ go test

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

MIT License

Copyright (c) 2020

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PaginatedData

type PaginatedData struct {
	Data       []bson.Raw     `json:"data"`
	Pagination PaginationData `json:"pagination"`
}

PaginatedData struct holds data and pagination detail

type PaginationData

type PaginationData struct {
	Total         int `json:"total"`
	Page          int `json:"page"`
	PerPage       int `json:"perPage"`
	Prev          int `json:"prev"`
	Next          int `json:"next"`
	TotalPages    int `json:"totalPages"`
	RecordsOnPage int `json:"recordsOnPage"`
}

PaginationData struct for returning pagination stat

type Paginator

type Paginator struct {
	TotalRecord int `json:"total_record"`
	TotalPage   int `json:"total_page"`
	Offset      int `json:"offset"`
	Limit       int `json:"limit"`
	Page        int `json:"page"`
	PrevPage    int `json:"prev_page"`
	NextPage    int `json:"next_page"`
}

Paginator struct for holding pagination info

func Paging

func Paging(p *PagingQuery) *Paginator

Paging returns Paginator struct which hold pagination stats

func (*Paginator) PaginationData

func (p *Paginator) PaginationData() *PaginationData

PaginationData returns PaginationData struct which holds information of all stats needed for pagination

type PagingQuery

type PagingQuery struct {
	Collection *mongo.Collection
	Filter     bson.M
	Projection *bson.D
	SortField  *string
	SortValue  *int
	Limit      int
	Page       int
}

PagingQuery struct for holding mongo connection, filter needed to apply filter data with page, limit, sort key and sort value

func (*PagingQuery) Find

func (paging *PagingQuery) Find() (paginatedData *PaginatedData, err error)

Find returns two value pagination data with document queried from mongodb and error if any error occurs during document query

Jump to

Keyboard shortcuts

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