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

mongoose-paginated-advance-query-middleware

v1.0.1

Published

Middleware for advanced results with pagination, sorting and field selection

Downloads

1

Readme

Mongoose Paginated Advanced Query Middleware

This middleware for Express.js allows you to easily implement advanced querying, sorting, field selection, and pagination on your Mongoose models.

Why Use It?

If you are building a REST API with Express.js and Mongoose, you'll often need to implement features such as querying, sorting, field selection, and pagination. Implementing these features manually for every route can be time-consuming and error-prone. This middleware abstracts away these complexities so you can implement these features with just a few lines of code.

Installation

Before you can use the Mongoose Paginated Advanced Query Middleware, you need to install it. You can do this using npm:

npm install mongoose-paginated-advance-query-middleware

Usage

After installation, you can require the middleware in your file:

const advanceQueryResults = require('mongoose-paginated-advance-query-middleware');

You can then use it in your routes:

app.get('/api/data', advanceQueryResults(Model), (req, res, next) => {
  res.status(200).json(res.advanceQueryResults);
});

Replace Model with the Mongoose model for which you want to get the advanced results.

The middleware function accepts two parameters:

  1. model: The Mongoose model to apply the advanced results on. This is a required parameter.

  2. populate: An array of objects specifying the paths to populate. Each object should have a path property and an optional select property. This parameter is optional.

Here's an example of using the middleware with the populate parameter:

app.get(
  '/api/data', 
  advanceQueryResults(Model, [{ path: 'field1', select: 'subfield1' }, { path: 'field2' }]), 
  (req, res, next) => {
    res.status(200).json(res.advanceQueryResults);
  }
);

Another example with complex or nested object populate:

app.get(
  '/api/data', 
  advanceQueryResults(Model, [{ path: 'field1', select: 'subfield1', populate: { path: 'field1', select:'subfield1'} },
  { path: 'field2' }]),
  (req, res, next) => {
    res.status(200).json(res.advanceQueryResults);
  }
);

The middleware adds an advanceQueryResults object to the response (res) object. This object has the following properties:

  • success: A boolean indicating whether the operation was successful.
  • count: The number of documents returned.
  • pagination: An object containing information about the next and previous pages (if they exist).
  • data: An array containing the documents returned.

Features

Field Selection

By using the select query parameter, you can specify which fields you want to be returned in the data. For example:

GET /api/users?select=name,email

Sorting

You can sort the results using the sort query parameter. The value of this parameter should be the field you want to sort by. For example:

GET /api/users?sort=name

To sort in descending order, prepend the field name with a -.

GET /api/users?sort=-name

Pagination

The middleware supports pagination through the page and limit query parameters. For example:

GET /api/users?page=2&limit=10

Filtering

The middleware supports several comparison operators for filtering: gt (greater than), gte (greater than or equal), lt (less than), lte (less than or equal), and in (in array). For example:

GET /api/users?age[gt]=20
GET /api/users?role[in]=admin,user

Combination of Features

You can combine several features in a single request. For example:

GET /api/users?role=admin&sort=-name&select=name,email&page=2&limit=10

Dependencies

Mongoose Paginated Advanced Query Middleware depends on:

  • Express.js: A web application framework for Node.js.
  • Mongoose: An Object Data Modeling (ODM) library for MongoDB and Node.js.

License

This middleware is licensed under the ISC license.

Author

This middleware was created by Arslan Ameer.

Contributing

Contributions are always welcome! Please feel free to open an issue or create a pull request if you have any improvements or feature requests on the GitHub repository.