Menquery
Querystring parser middleware for MongoDB, Express and Nodejs
Install
npm install --save menquery
Examples
Pagination
Menquery has a default schema to handle pagination. This is the most simple and common usage.
var menquery = ; app;
User requests /posts?page=2&limit=20&sort=-createdAt
req.menquery will be:
reqmenquery = query: {} select: {} cursor: limit: 20 skip: 20 sort: createdAt: -1
User requests /posts?q=term&select=title,desc
req.menquery will be:
reqmenquery = query: keywords: /term/i select: title: 1 desc: 1 cursor: // defaults limit: 30 skip: 0 sort: name: 1
User requests /posts?select=-title&sort=name,-createdAt
req.menquery will be:
reqmenquery = 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 menquery default schema (explained above).
var menquery = ; app;
User requests /posts?after=2016-04-23
req.menquery will be:
reqmenquery = query: createdAt: $gte: 1461369600000 select: {} cursor: // defaults limit: 30 skip: 0 sort: name: 1
Reusable schemas
You can create reusable schemas as well. Just instantiate a menquery.Schema
object.
var menquery = ; var schema = tags: type: String ; // user requests /posts?tags=world,travel// req.menquery.query is {tags: {$in: ['world', 'travel']}}app;app;
Advanced schema
var menquery = ; var schema = active: Boolean // shorthand to {type: Boolean} sort: '-createdAt' // shorthand to {type: String, default: '-createdAt'} term: type: RegExp paths: 'title' 'description' bindTo: 'search' // default was 'query' with_picture: type: Boolean paths: 'picture' operator: '$exists' page: false // disable default parameter `page` limit: 'max_items' // change name of default parameter `limit` to `max_items`; app;
Dynamic advanced schema
var menquery = ;var schema = ; schema; schema; // {type: String}schema; // {type: String, scream: true}schemavalue'help';console; // HELP!!!!!!! schema; schema; // {type: String, scream: true, isPlural: true}console; // falseschema;console; // trueconsole; // HELPS!!!!!!! schema; schema;console; // {text: 'IVEGOTCONTROLS!!!!!!!'} schema;console; // {text: {$elemMatch: {prop: {$eq: 'IVEGOTCONTROLS!!!!!!!'}}}}
Error handling
// user requests /posts?category=worldvar menquery = ; var schema = category: type: String enum: 'culture' 'general' 'travel' ; app; app;
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