Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bitnbytesio/paginator-for-mongoose
A simple pagination library for mongoose
https://github.com/bitnbytesio/paginator-for-mongoose
Last synced: about 1 month ago
JSON representation
A simple pagination library for mongoose
- Host: GitHub
- URL: https://github.com/bitnbytesio/paginator-for-mongoose
- Owner: bitnbytesio
- Created: 2019-04-26T12:14:22.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-08T23:05:02.000Z (over 3 years ago)
- Last Synced: 2024-10-12T04:26:59.749Z (2 months ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Paginator For Mongoose
This library provides elegent way to paginate both simple and aggregate mongoose query.
## Installation
```shell
npm install --save paginator-for-mongoose
```## Usage
Simple Query
```javascript
const {paginate} = require('paginator-for-mongoose');
// usage with some mongoose model
let page = 1;
// limit is optional
const limit = 15;const results = await paginate(Model.find({status: 'active'}), page, limit);
```
With Aggregate
```javascript
const {paginateAggregate} = require('paginator-for-mongoose');
// usage with some mongoose model
let page = 1;
// limit is optional
const limit = 15;const results = await paginateAggregate(Model.aggregate([{ $match: {status: 'active'} }]), page, limit);
```
Usage with Koa
```javascript
const mongoose = require('mongoose');
const {enablePlugin, plugin} = require('paginator-for-mongoose');// call this method to use schema plugin
enablePlugin(mongoose);// or you can enable like mongoose.plugin(plugin);
// example usage in koajs controller
let page = 1;
// limit is optional, by default 15 items per page
// to change limit set ctx.PER_PAGE = 50, this will set 50 items per pageconst results = await Model.aggregate([{ $match: {status: 'active'} }]).paginateAggregate(ctx);
// or for simple query
const results = await Model.find({status: 'active'}).paginate(ctx);```
Using plugin on specific models
```javascript
const mongoose = require('mongoose');
const {plugin} = require('paginator-for-mongoose');Model.plugin(plugin);
// example usage in koajs controller
let page = 1;
// limit is optional
const limit = 15;const results = await Model.aggregate([{ $match: {status: 'active'} }]).paginateAggregate(ctx);
// or for simple query
const results = await Model.find({status: 'active'}).paginate(ctx);```
## Ver 3
Paginate Using ID for large collections
```javascript
paginateById(User.find({ role: 'user' }))
.then((docs) => {
console.log(docs);
})
.catch((e) => console.error(e));
```To paginate, pass last document id as second parameter. Third parameter is limit (Default: 50).
```javascript
// pass last document id as second parameter
paginateById(User.find({ role: 'user' }), '5e25a641beac1d5978ad2c14')
.then((docs) => {
console.timeEnd('Pagi');
console.log(docs);
})
.catch((e) => console.error(e));
```Using aggregate query. Second argument is id of last docuemnt and is optional for first page.
```javascript
paginateAggregateById(User.aggregate([{ $match: { role: 'user' } }]), '5e25a641beac1d5978ad2c14')
.then((docs) => {
console.log(docs);
})
.catch((e) => console.error(e));
```Note: Pagination using id does not returns documents count because count is slow on large collections.