Every web developer should work on pagination at some point. We were using Mongo DB and node.js, and surprised by the fact that there is no single package we can just install and start using. If you're using Mongoose on Node.js and going to work on pagination? Then node-paginator is worth trying.
npm install @careerday-jobs/node-paginator
import { NodePaginator } from '@careerday-jobs/node-paginator';
// Mongoose model you're going to look up
import { UserModel } from '/src/models/user.model.ts';
const paginatedResult = await NodePaginator.paginateByMongoose({
pageNoParam: 1, // required
pageSizeParam: 10, // required
model: UserModel, // required
});
const { pageNo, pageSize, totalItemNo, totalPageNo, items } = paginatedResult;
- Use regex feature provided by MongoDB for simple search which is useful enough.
import { NodePaginator } from '@careerday-jobs/node-paginator';
import { UserModel } from '/src/models/user.model.ts';
const paginatedResult = await NodePaginator.paginateByMongoose({
pageNoParam: 1, // required
pageSizeParam: 10, // required
model: UserModel, // required
filteredFields: [
'username',
'email',
'address'
], // field names you are trying to look up
filter: 'michael' // search keyword
});
const { pageNo, pageSize, totalItemNo, totalPageNo, items } = paginatedResult;
- Add and queries and or queries you want to add on
import { NodePaginator } from '@careerday-jobs/node-paginator';
import { UserModel } from '/src/models/user.model.ts';
const paginatedResult = await NodePaginator.paginateByMongoose({
pageNoParam: 1,
pageSizeParam: 10,
model: UserModel,
filteredFields: [
'username',
],
filter: 'michael',
orQuery: { email: 'sample@gmail.com'}, // or query. (should get user of which email address is 'sample@gmail.com', even if username does not include 'michael')
andQuery: { company: 'google' } // and query (username should include 'michael' and he/she is working for google)
});
const { pageNo, pageSize, totalItemNo, totalPageNo, items } = paginatedResult;
- Putting sort option is possible. (Default is
{ _id: -1 }
)
import { NodePaginator } from '@careerday-jobs/node-paginator';
// Mongoose model you're going to look up
import { UserModel } from '/src/models/user.model.ts';
const paginatedResult = await NodePaginator.paginateByMongoose({
pageNoParam: 1,
pageSizeParam: 10,
model: UserModel,
filteredFields: [
'username',
],
filter: 'michael',
orQuery: { email: 'sample@gmail.com'},
andQuery: { company: 'google' },
sortingOption: { createdAt: 1 } // show the oldest one first
});
const { pageNo, pageSize, totalItemNo, totalPageNo, items } = paginatedResult;
- Default maximum size of page is 100. But if you want it to exceed the limit, you can put one more parameter at the end.
import { NodePaginator } from '@careerday-jobs/node-paginator';
// Mongoose model you're going to look up
import { UserModel } from '/src/models/user.model.ts';
const paginatedResult = await NodePaginator.paginateByMongoose({
pageNoParam: 1,
pageSizeParam: 1000, // PLEASE DON'T DO THIS.....BUT SOMETIMES YOU NEED TO DO IT
model: UserModel,
filteredFields: [
'username',
],
filter: 'michael',
orQuery: { email: 'sample@gmail.com'},
andQuery: { company: 'google' },
sortingOption: { createdAt: 1 },
isNoMinResultLimit: true // If you want the page size to exceed 100, you should set this true
});
const { pageNo, pageSize, totalItemNo, totalPageNo, items } = paginatedResult;
parameter | type | description |
---|---|---|
pageNoParam (required) |
number |
current page number |
pageSizeParam (required) |
numer |
maximum number of data to display on a page |
model (required) |
Model<any> |
Mongoose model instance you want to look up |
filteredFields | string[] |
field names of the model you want to look up |
filter | string |
search keyword |
orQuery | FilterQuery |
custom OR query |
andQuery | FilterQuery |
custom AND query |
sortingOption | SortingOption |
sort options applied to pagination results. (default: { _id: -1 } ) |
isNoMinResultLimit | boolean |
If page size can exceed 100 (default: false) |
PaginatedResult {
pageNo: 1,
pageSize: 20,
totalItemNo: 100,
totalPageNo: 5,
items: [
{
_id: new ObjectId("632a8d60d7b81199ad39d674"),
email: 'Tony14@yahoo.com',
name: 'Hermann80',
age: 71
},
{
_id: new ObjectId("632a8d60d7b81199ad39d673"),
email: 'Romaine_Wilkinson@gmail.com',
name: 'Agustina7',
age: 58
},
...
]
}
Looking for a library which supports pagination on frontend? Why don't you check out react-paginator? It's way more efficient when you try both packages!
MIT @ 2022 CareerDay