@careerday-jobs/node-paginator
v0.0.17
Published
Node.js-based custom pagination library
Downloads
4
Maintainers
Readme
📚 Why node-paginator?
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.
📦 Install
npm install @careerday-jobs/node-paginator
🪄 Basic Usage
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;
🪄 Advanced Usage
- 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: '[email protected]'}, // or query. (should get user of which email address is '[email protected]', 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: '[email protected]'},
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: '[email protected]'},
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;
Input Parameters
| 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) |
Output Example
PaginatedResult {
pageNo: 1,
pageSize: 20,
totalItemNo: 100,
totalPageNo: 5,
items: [
{
_id: new ObjectId("632a8d60d7b81199ad39d674"),
email: '[email protected]',
name: 'Hermann80',
age: 71
},
{
_id: new ObjectId("632a8d60d7b81199ad39d673"),
email: '[email protected]',
name: 'Agustina7',
age: 58
},
...
]
}
⏳ Pagination Library On Frontend
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!
Contributors
@kunhokimcareerday
@starseeder0309
🔑 License
MIT @ 2022 CareerDay