mongopagination

package module
v0.0.0-...-d27d5f2 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 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/gobeam/mongo-go-pagination

or with dep

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

Demo

package main

import (
	"context"
	. "github.com/gobeam/mongo-go-pagination"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"time"
)

type ToDo struct {
	TaskName string `json:"task_name"`
	TaskStatus string `json:"task_status"`
}

func main() {
	// Establishing mongo db connection
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		panic(err)
	}

	// making example filter
	filter := bson.M{}
	filter["status"] = 1
	
	var limit int64 = 10
	var page int64 = 1
	
	// Querying paginated data
	paging := PagingQuery{
		Collection: client.Database("db_name").Collection("collection_name"),
		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 []ToDo
	for _, raw := range paginatedData.Data {
		var todo ToDo
		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     int64 `json:"total"`
	Page      int64 `json:"page"`
	PerPage   int64 `json:"perPage"`
	Prev      int64 `json:"prev"`
	Next      int64 `json:"next"`
	TotalPage int64 `json:"totalPage"`
}

PaginationData struct for returning pagination stat

type Paginator

type Paginator struct {
	TotalRecord int64 `json:"total_record"`
	TotalPage   int64 `json:"total_page"`
	Offset      int64 `json:"offset"`
	Limit       int64 `json:"limit"`
	Page        int64 `json:"page"`
	PrevPage    int64 `json:"prev_page"`
	NextPage    int64 `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     interface{}
	SortField  string
	SortValue  int
	Limit      int64
	Page       int64
}

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