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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoose-rest-utils

v5.7.1

Published

Mongoose utils functions for REST API

Readme

mongoose-REST-utils

Installation

$ npm install -g mongoose-rest-utils

Usage

Utils allow you to perform basic and more or less complexe GET POST PUT and DELETE operations with mongoose !

The 4 methods (get, post, put and delete) take 4 arguments :

  • req which is the request object
  • res which is the response object
  • Model which is the mongoose model you want to use
  • an optional success callback method that take three arguments req, res, Array || CreatedEntity || UpdatedEntity. If no callback is given, the method send a response with the new object, the updated object or the entity array returned in case of POST, PUT or GET.

###GET

####PAGINATION The GET method can take severals parameters from the query object of the request for pagination:

req.query['limit'] = 5; //integer maximum number of result returned, same as SQL's limit
req.query['page'] = 5 //integer page you want to access, starting from 0, each page contains 'limit' result

####COUTING If you want to get a SQL COUNT equivalent, just provide this into the query :

req.query['count'] = true;

####FILTERING

It also takes severals parameter for filtering and/or populating field on the entity you request. For example let's say I want to get people whose firstname are John, exclude their age from the result, oh and ones with "Smith" lastname are ignored:

req.query["firstname"] = "John"; //firstname being the attribut in your model
req.query["no_age"] = true;
req.query["not_lastname"] = "Smith"; //case incensitive

####POPULATING

For now, utils provides populating up to 2 nesting.

0 LEVEL DEEP (populate attribute)

Let's say I want to populate a car attribute of my entity :

req.query["populate_car"] = true;
1 LEVEL DEEP (populate attribute nested inside populated attribute)

Now I want to populate the car's model attribute at the same time :

req.query["populate_nested_car"] = "model";
2 LEVEL DEEP (populate attribute nested inside populated attribute nested inside populate attribute)

Because nesting never stop, I want to populate the model's brand attribute :

req.query["populate_nested_deep_car_model"] = "brand";

However I'm sure there is a recursive way to deal with nested populate, not went into it so far.

###POST

The POST just create an entity passed into a "data" attribute from body.

req.body.data = {firstname : "John", lastname :"Doe"};

###PUT

The PUT just update an entity passed into a "data" attribute from body.

req.body.data = {_id : "e654fefeacde654", firstname : "John", lastname :"Doe"};

###DELETE

The DELETE just delete an entity with the id given in the url params attribute

 Model.findOneAndRemove({_id: req.params.id}, function (err) {
        if (err)
            return res.status(500).json({success: false, data: err});
        return res.status(200).json({success: true});
    });