Querymen
Querystring parser middleware for MongoDB, Express and Nodejs
Install
npm install --save querymen
Examples
Pagination
Querymen has a default schema to handle pagination. This is the most simple and common usage.
; app;
User requests /posts?page=2&limit=20&sort=-createdAt
querymen will be:
querymen = query: {} select: {} cursor: limit: 20 skip: 20 sort: createdAt: -1
User requests /posts?q=term&fields=title,desc
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).
querymen = 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
querymen will be:
querymen = 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).
; app;
User requests /posts?after=2016-04-23
querymen will be:
querymen = query: createdAt: $gte: 1461369600000 select: {} cursor: // defaults limit: 30 skip: 0 sort: createdAt: -1
Reusable schemas
You can create reusable schemas as well. Just instantiate a Schema
object.
; const schema = tags: type: String ; // user requests /posts?tags=world,travel// querymen.query is { tags: { $in: ['world', 'travel'] }}app;app;
Advanced schema
; const 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
;const 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!!!!!!!'} }}}
Geo queries
Querymen also support geo queries, but it's disabled by default. To enable geo queries you just need to set near
option to true in schema options.
; app;
Its paths
option is set to ['location']
by default, but you can change this as well:
; app;
User requests /places?near=-22.332113,-44.312311
(latitude, longitude), req.querymen.query will be:
reqquerymenquery = loc: $near: $geometry: type: 'Point' coordinates: -44312311 -22332113
User requests /places?near=-22.332113,-44.312311&min_distance=200&max_distance=2000
(min_distance and max_distance in meters), req.querymen.query will be:
reqquerymenquery = loc: $near: $geometry: type: 'Point' coordinates: -44312311 -22332113 $minDistace: 200 $maxDistance: 2000
You can also use legacy geo queries as well. Just set geojson
option in param:
; app;
User requests /places?near=-22.332113,-44.312311&min_distance=200&max_distance=2000
, req.querymen.query will be:
reqquerymenquery = loc: $near: -44312311 -22332113 // convert meters to radians automatically $minDistace: 0000031 $maxDistance: 000031
Error handling
// user requests /posts?category=world; const 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