Querymen
Querystring parser middleware for MongoDB, Express and Nodejs
Important: This is a fork from 'querymen'
https://github.com/diegohaz/querymen
Install
npm install --save querymen-custom
Examples
Pagination
Querymen has a default schema to handle pagination. This is the most simple and common usage.
var querymen = ; app;
User requests /posts?page=2&limit=20&sort=-createdAt
req.querymen will be:
reqquerymen = query: {} select: {} cursor: limit: 20 skip: 20 sort: createdAt: -1
User requests /posts?q=term&fields=title,desc
req.querymen will be:
When user requests
/posts?q=term
, querymen parses it to{keywords: /term/i}
. It was designed to work with mongoose-keywords plugin, which adds akeywords
field to schemas (check that out).
reqquerymen = query: keywords: /term/i select: title: 1 desc: 1 cursor: // defaults limit: 30 skip: 0 sort: createdAt: -1
User requests /posts?fields=-title&sort=name,-createdAt
req.querymen will be:
reqquerymen = query: {} select: title: 0 cursor: limit: 30 skip: 0 sort: name: 1 createdAt: -1
Custom schema
You can define a custom schema, which will be merged into querymen default schema (explained above).
var querymen = ; app;
User requests /posts?after=2016-04-23
req.querymen will be:
reqquerymen = query: createdAt: $gte: 1461369600000 select: {} cursor: // defaults limit: 30 skip: 0 sort: createdAt: -1
Error handling
// user requests /posts?category=worldvar querymen = ; var schema = category: type: String enum: 'culture' 'general' 'travel' ; app; // create your own handlerapp; // or use querymen error handlerapp
Response body will look like:
Contributing
This package was created with generator-rise. Please refer to there to understand the codestyle and workflow. Issues and PRs are welcome!
License
MIT © Diego Haz