0.0.17 • Published 7 months ago

@careerday-jobs/node-paginator v0.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

npm version npm downloads official website

📚 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

  1. 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;
  1. 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;
  1. 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;
  1. 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;

Input Parameters

parametertypedescription
pageNoParam (required)numbercurrent page number
pageSizeParam (required)numermaximum number of data to display on a page
model (required)Model<any>Mongoose model instance you want to look up
filteredFieldsstring[]field names of the model you want to look up
filterstringsearch keyword
orQueryFilterQuerycustom OR query
andQueryFilterQuerycustom AND query
sortingOptionSortingOptionsort options applied to pagination results. (default: { _id: -1 })
isNoMinResultLimitbooleanIf page size can exceed 100 (default: false)

Output Example

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

⏳ 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

0.0.16

7 months ago

0.0.17

7 months ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago