node-mongoose-util
v2.1.0
Published
Utility library with helper function for mongoose and node
Maintainers
Readme
Node Mongoose Util
A nodejs and mongoose utility library,it consists of various utilities to assist in mongoose-nodejs application.
Installing
npm i --save node-mongoose-utilUsage
Sample example
const nodeMongooseUtil = require('node-mongoose-util');
const User = mongoose.model('User');
/**
* Handle Http GET on /users
*/
router.get('/users', (request, response, next) => {
const { query, fields, page, limit } = nodeMongooseUtil.parseQuery(request.query);
User.find(query)
});API
nodeMongooseUtil.parseQuery(params) => Object
It takes in query params from user and parse it to make them mongo friendly. It has the following characteristics;
- Pagination support
It will not includepageandlimitfields in the result query if supplied. - Not equal clause
If the query param field value preceded byneq|, it will output$neoperator query clause. - Include query clause
It can create$inquery clause for field contain comma seaprated values and included inparams.$inkey.
params
params.query[required] - Object
Required. Specifies the query object to be parsedparams.$in - String[] Array of field names which can be transformed to
$inquery clause if they have comma separated values
Return
Object with the following properties
- query - object
Mongoose friendly criteria object - fields - string
Space separated field names to select - sort - string
Space separated field names to use for sorting - page - number
Page number used with pagination - limit - number
Result limit used with pagination
