npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-mongoose-pagination

v2.2.0

Published

A light weight package used along with mongoose to query collections almost similar to Laravel pagination.

Downloads

15

Readme

Introduction


Hello Folks,

Pagination of data is highly recommended if you want to fetch large number of records from the collection. For eg: All users from Users collection. If you fetch the records without pagination, it may result in slow performance at both back-end and front-end side.

So, this tool will help you to paginate your data without impacting the performance.

You need to use this tool with find() query method of mongoose ODM to make it work.

You just need to pass the request object, and some optional parameters to the paginate() method of this tool, it will return an object with useful information that you can use to handle you data with ease.

I have explained it's implementation below:

Installation


Install with the following command

npm i express-mongoose-pagination or npm install express-mongoose-pagination

Implementation


1. Import the module in model

//UserModel.js

const pagination = require('express-mongoose-pagination')

or

//ES6+
import pagination from 'express-mongoose-pagination'

2. Apply as plugin to your Schema Instance

//UserModel.js

const userSchema = new Schema({...})

userSchema.plugin(pagination)

module.exports = mongoose.model('User', userSchema)

or

//ES6+
export default mongoose.model('User', userSchema)

3. Somewhere inside your routes block

//routes.js
const User = require('../models/UserModel.js')

or

import User from '../models/UserModel.js'

//Users route get request
app.get('/users', async function(req, res){

   //1. Use the find query method normally without any restrictions. You can pass all available parameters like condition, projection, options, etc

   const users = await User.find().paginate(req, { withQueryString: true })

   res.render('views/users.html', { users }) //render and pass users

})

That's It

Data Object and Query Parameters


You will get the output data in the form of object as mentioned below:

//console.log(users)

{
   currentPage: 1// Current page number,
   data: [
      {_id:1, name:'User 1', email: '[email protected]'},
      {...},
      {...},
      .
      .
      .
      //upto 15 records

   ] // Array of data objects,
   firstPageUrl: 'https://xyz.com/users?page=1',
   lastPageUrl: 'https://xyz.com/users?page=4',
   lastPage: 4 // Last page number,
   nextPageUrl: 'https://xyz.com/users?page=2',
   prevPageUrl: null,
   path: 'https://xyz.com/users',
   perPage: 15 //Per page value,
   total: 60 //Total matched records in users collection
}

//If there is no records you will get same object format with changed values and most of them will be null or empty array for data field.

Pre-defined parameters:

Custom parameters:

You can pass n number of query parameters with your endpoint and that will reflect in all url specific fields except path field.

Note: By default custom query string option is disabled, you can enable it by passing withQueryString as a boolean value

//pass withQueryString option like this

const users = await User.find({ sortIndex: {$gt: 10 } })
                .sort({ sortIndex: 1 })
                .paginate(request, { withQueryString:true })

// Output will be like this
{
   currentPage:1,
   .
   .
   firstPageUrl: 'https://xyz.com/users?page=1&perPage=10&foo=bar&john=doe',
   lastPageUrl: 'https://xyz.com/users?page=4&perPage=10&foo=bar&john=doe',
   .
   .

}                

Serial Numbers:

You can pass _sno as boolean to options parameters object to paginate function to get data with serial numbers.

//pass _sno option like this

const users = await User.find({ sortIndex: {$gt: 10 } })
                .sort({ sortIndex: 1 })
                .paginate(request, { withQueryString:true, _sno:true })

// Output will be like this
{
   currentPage:1,
   data:[
      {
         ...
         ...
         _sno: 1
      },
      {
         ...
         ...
         _sno: 2
      }
      ....
   ]
   .
   firstPageUrl: 'https://xyz.com/users?page=1&perPage=10&foo=bar&john=doe',
   lastPageUrl: 'https://xyz.com/users?page=4&perPage=10&foo=bar&john=doe',
   .
   .

}                

License


MIT

Show your Support


If you like my work, please support and share it to your fellow people.

Thank you and have a great time ahead!