New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mongoose-rest-utils

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-rest-utils

Mongoose utils functions for REST API

latest
Source
npmnpm
Version
5.7.1
Version published
Maintainers
1
Created
Source

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});
    });

FAQs

Package last updated on 21 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts