All Projects → aravindnc → mongoose-aggregate-paginate-v2

aravindnc / mongoose-aggregate-paginate-v2

Licence: MIT license
A cursor based custom aggregate pagination library for Mongoose with customizable labels.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mongoose-aggregate-paginate-v2

Mongoose Paginate V2
A cursor based custom pagination library for Mongoose with customizable labels.
Stars: ✭ 283 (+174.76%)
Mutual labels:  pagination, mongoose, callback, cursor, paging
mongoose-graphql-pagination
GraphQL cursor pagination (Relay-like) for Mongoose models.
Stars: ✭ 29 (-71.84%)
Mutual labels:  pagination, mongoose, cursor
React Paginate
A ReactJS component that creates a pagination
Stars: ✭ 2,169 (+2005.83%)
Mutual labels:  pagination, paginator, paginate
Nedb Promises
A dead-simple promise wrapper for nedb.
Stars: ✭ 190 (+84.47%)
Mutual labels:  promise, callback, cursor
lampager-laravel
Rapid pagination for Laravel
Stars: ✭ 71 (-31.07%)
Mutual labels:  pagination, paginator
paginathing
a jQuery plugin to paginate your DOM easily.
Stars: ✭ 23 (-77.67%)
Mutual labels:  pagination, paginate
reactionmenu
A library to create a discord paginator. Supports pagination with Discords Buttons feature and reactions.
Stars: ✭ 68 (-33.98%)
Mutual labels:  pagination, paginator
vue-tiny-pagination
A Vue component for create a tiny pagination with Flexbox
Stars: ✭ 20 (-80.58%)
Mutual labels:  pagination, paginate
fast-relay-pagination
Improve relay pagination performance with find and limit
Stars: ✭ 18 (-82.52%)
Mutual labels:  pagination, mongoose
uni-z-paging
【uni-app自动分页器】超简单!仅需两步轻松完成完整分页逻辑(下拉刷新、上拉加载更多),分页全自动处理。支持自定义加载更多的文字或整个view,自定义下拉刷新样式,自动管理空数据view等。
Stars: ✭ 91 (-11.65%)
Mutual labels:  pagination, paging
Dotnetpaging
Data paging with ASP.NET and ASP.NET Core
Stars: ✭ 70 (-32.04%)
Mutual labels:  pagination, paging
Combine Pagination
A JavaScript library for paginating data from multiple sources 🦑
Stars: ✭ 157 (+52.43%)
Mutual labels:  pagination, paging
go-paginate
Cursor-based go paginator
Stars: ✭ 48 (-53.4%)
Mutual labels:  pagination, cursor
RxPagingLoading
Easy handling of the Paging or Loading screens states
Stars: ✭ 45 (-56.31%)
Mutual labels:  pagination, paging
express-mquery
Expose mongoose query API through HTTP request.
Stars: ✭ 37 (-64.08%)
Mutual labels:  pagination, mongoose
nodeJS examples
Server, routing, db examples using NodeJS v6
Stars: ✭ 34 (-66.99%)
Mutual labels:  mongoose, promise
Escape From Callback Mountain
Example Project & Guide for mastering Promises in Node/JavaScript. Feat. proposed 'Functional River' pattern
Stars: ✭ 249 (+141.75%)
Mutual labels:  promise, callback
Amazon Mws
Amazon MWS NodeJS Wrapper
Stars: ✭ 196 (+90.29%)
Mutual labels:  promise, callback
scrivener list
A Scrivener compatible extension that allows pagination of a list of elements.
Stars: ✭ 23 (-77.67%)
Mutual labels:  pagination, paginate
dhan-gaadi
A complete online bus reservation system (Node, React, Mongo, NextJS, ReactNative)
Stars: ✭ 207 (+100.97%)
Mutual labels:  mongoose, next

mongoose-aggregate-paginate-v2

npm version Build Status contributions welcome Downloads

A cursor based custom aggregate pagination library for Mongoose with customizable labels.

If you are looking for basic query pagination library without aggregate, use this one mongoose-paginate-v2

Installation

npm install mongoose-aggregate-paginate-v2

Usage

Adding the plugin to a schema,

var mongoose = require("mongoose");
var aggregatePaginate = require("mongoose-aggregate-paginate-v2");

var mySchema = new mongoose.Schema({
  /* your schema definition */
});

mySchema.plugin(aggregatePaginate);

var myModel = mongoose.model("SampleModel", mySchema);

and then use model aggregatePaginate method,

// as Promise

var myModel = require("/models/samplemodel");

const options = {
  page: 1,
  limit: 10,
};

var myAggregate = myModel.aggregate();
myModel
  .aggregatePaginate(myAggregate, options)
  .then(function (results) {
    console.log(results);
  })
  .catch(function (err) {
    console.log(err);
  });
// as Callback

var myModel = require('/models/samplemodel');

const options = {
    page: 1,
    limit: 10
};

var myAggregate = myModel.aggregate();
myModel.aggregatePaginate(myAggregate, options, function(err, results) {
	if(err) {
		console.err(err);
	else {
    	console.log(results);
	}
})
// Execute pagination from aggregate
const myModel = require('/models/samplemodel');

const options = {
    page: 1,
    limit: 10
};

const myAggregate = myModel.aggregate();
myAggregate.paginateExec(options, function(err, results) {
	if(err) {
		console.err(err);
	else {
    	console.log(results);
	}
})

Model.aggregatePaginate([aggregateQuery], [options], [callback])

Returns promise

Parameters

  • [aggregate-query] {Object} - Aggregate Query criteria. Documentation
  • [options] {Object}
    • [sort] {Object | String} - Sort order. Documentation
    • [offset=0] {Number} - Use offset or page to set skip position
    • [page] {Number} - Current Page (Defaut: 1)
    • [limit] {Number} - Docs. per page (Default: 10).
    • [customLabels] {Object} - Developers can provide custom labels for manipulating the response data.
    • [pagination] {Boolean} - If pagination is set to false, it will return all docs without adding limit condition. (Default: True)
    • [allowDiskUse] {Bool} - To enable diskUse for bigger queries. (Default: False)
    • [countQuery] {Object} - Aggregate Query used to count the resultant documents. Can be used for bigger queries. (Default: aggregate-query)
    • [useFacet] {Bool} - To use facet operator instead of using two queries. This is the new default. (Default: true)
  • [callback(err, result)] - (Optional) If specified the callback is called once pagination results are retrieved or when an error has occurred.

Return value

Promise fulfilled with object having properties:

  • docs {Array} - Array of documents
  • totalDocs {Number} - Total number of documents that match a query
  • limit {Number} - Limit that was used
  • page {Number} - Current page number
  • totalPages {Number} - Total number of pages.
  • offset {Number} - Only if specified or default page/offset values were used
  • hasPrevPage {Bool} - Availability of prev page.
  • hasNextPage {Bool} - Availability of next page.
  • prevPage {Number} - Previous page number if available or NULL
  • nextPage {Number} - Next page number if available or NULL
  • pagingCounter {Number} - The starting sl. number of first document.
  • meta {Object} - Object of pagination meta data (Default false).

Please note that the above properties can be renamed by setting customLabels attribute.

Sample Usage

Return first 10 documents from 100

const options = {
  page: 1,
  limit: 10,
};

// Define your aggregate.
var aggregate = Model.aggregate();

Model.aggregatePaginate(aggregate, options)
  .then(function (result) {
    // result.docs
    // result.totalDocs = 100
    // result.limit = 10
    // result.page = 1
    // result.totalPages = 10
    // result.hasNextPage = true
    // result.nextPage = 2
    // result.hasPrevPage = false
    // result.prevPage = null
  })
  .catch(function (err) {
    console.log(err);
  });

With custom return labels

Now developers can specify the return field names if they want. Below are the list of attributes whose name can be changed.

  • totalDocs
  • docs
  • limit
  • page
  • nextPage
  • prevPage
  • totalPages
  • hasNextPage
  • hasPrevPage
  • pagingCounter
  • meta

You should pass the names of the properties you wish to changes using customLabels object in options. Labels are optional, you can pass the labels of what ever keys are you changing, others will use the default labels.

If you want to return paginate properties as a separate object then define customLabels.meta.

Same query with custom labels

const myCustomLabels = {
  totalDocs: 'itemCount',
  docs: 'itemsList',
  limit: 'perPage',
  page: 'currentPage',
  nextPage: 'next',
  prevPage: 'prev',
  totalPages: 'pageCount',
  hasPrevPage: 'hasPrev',
  hasNextPage: 'hasNext',
  pagingCounter: 'pageCounter',
  meta: 'paginator'
};

const options = {
    page: 1,
    limit: 10,
    customLabels: myCustomLabels
};

// Define your aggregate.
var aggregate = Model.aggregate();

Model.aggregatePaginate(aggregate, options, function(err, result) {
if(!err) {
  // result.itemsList [here docs become itemsList]
  // result.itemCount = 100 [here totalDocs becomes itemCount]
  // result.perPage = 10 [here limit becomes perPage]
  // result.currentPage = 1 [here page becomes currentPage]
  // result.pageCount = 10 [here totalPages becomes pageCount]
  // result.next = 2 [here nextPage becomes next]
  // result.prev = null [here prevPage becomes prev]

  // result.hasNextPage = true [not changeable]
  // result.hasPrevPage = false [not changeable]
} else {
  console.log(err);
};

Using offset and limit

Model.aggregatePaginate(
  aggregate,
  { offset: 30, limit: 10 },
  function (err, result) {
    // result
  }
);

Using countQuery

// Define your aggregate query.
var aggregate = Model.aggregate();

// Define the count aggregate query. Can be different from `aggregate`
var countAggregate = Model.aggregate();

// Set the count aggregate query
const options = {
  countQuery: countAggregate,
};

Model.aggregatePaginate(aggregate, options)
  .then(function (result) {
    // result
  })
  .catch(function (err) {
    console.log(err);
  });

Global Options

If you want to set the pagination options globally across the model. Then you can do like below,

let mongooseAggregatePaginate = require("mongoose-aggregate-paginate-v2");

let BookSchema = new mongoose.Schema({
  title: String,
  date: Date,
  author: {
    type: mongoose.Schema.ObjectId,
    ref: "Author",
  },
});

BookSchema.plugin(mongooseAggregatePaginate);

let Book = mongoose.model("Book", BookSchema);

// Like this.
Book.aggregatePaginate.options = {
  limit: 20,
};

Release Note

v1.0.6 - Fixed exporting settings to global object.

v1.0.5 - Added meta attribute to return paginate meta data as a custom object.

v1.0.42 - Added optional countQuery parameter to specify separate count queries in case of bigger aggerate pipeline.

License

MIT

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].