Skip to content

Commit

Permalink
Added omit as an eligible query parameter. omit builds a fields objec…
Browse files Browse the repository at this point in the history
…t that omits specified fields and includes all others.
  • Loading branch information
rbarkas committed Sep 2, 2015
1 parent ec49a02 commit 1d091c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ For example, a query such as: `name=john&age>21&fields=name,age&sort=name,-age&o
}
}
```
The omit query parameter is used to create options: fields to exclude fields.
For example, a query such as: `name=john&age>21&omit=name&sort=name,-age&offset=10&limit=10` becomes the following hash:
```javascript
{
criteria: {
name: 'john',
age: { $gt: 21 }
},
options: {
fields: { age: false },
sort: { name: 1, age: -1 },
offset: 10,
limit: 10
}
}
```
The resulting query object can be used as parameters for a mongo collection query:
```javascript
var q2m = require('query-to-mongo')
Expand Down Expand Up @@ -86,7 +102,8 @@ The format for arguments was inspired by item #7 in [this article](http://blog.m

### Field selection
The _fields_ argument is a comma separated list of field names to include in the results. For example `fields=name,age` results in a _option.fields_ value of `{'name':true,'age':true}`. If no fields are specified then _option.fields_ is null, returning full documents as results.

The _omit_ argument is a comma separated list of field names to exclude in the results. For example `omit=name,age` results in a _option.fields_ value of `{'name':false,'age':false}`. If no fields are specified then _option.fields_ is null, returning full documents as results.
Note that either _fields_ or _omit can be used. If both are specified then _omit_ takes precedence and the _fields_ entry is ignored.
### Sorting
The _sort_ argument is a comma separated list of fields to sort the results by. For example `sort=name,-age` results in a _option.sort_ value of `{'name':1,'age':-1}`. If no sort is specified then _option.sort_ is null and the results are not sorted.

Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var querystring = require('querystring')
var iso8601 = require('./lib/iso8601-regex')

// Convert comma separate list to a mongo projection.
// Convert comma separated list to a mongo projection.
// for example f('field1,field2,field3') -> {field1:true,field2:true,field3:true}
function fieldsToMongo(fields) {
if (!fields) return null
Expand All @@ -12,7 +12,18 @@ function fieldsToMongo(fields) {
return hash
}

// Convert comma separate list to mongo sort options.
// Convert comma separated list to a mongo projection which specifies fields to omit.
// for example f('field2') -> {field2:false}
function omitFieldsToMongo(omitFields) {
if (!omitFields) return null
var hash = {}
omitFields.split(',').forEach(function(omitField) {
hash[omitField.trim()] = false
})
return hash
}

// Convert comma separated list to mongo sort options.
// for example f('field1,+field2,-field3') -> {field1:1,field2:1,field3:-1}
function sortToMongo(sort) {
if (!sort) return null
Expand Down Expand Up @@ -110,11 +121,13 @@ function queryCriteriaToMongo(query, options) {
function queryOptionsToMongo(query, options) {
var hash = {},
fields = fieldsToMongo(query.fields),
omitFields = omitFieldsToMongo(query.omit),
sort = sortToMongo(query.sort),
maxLimit = options.maxLimit || 9007199254740992,
limit = options.maxLimit || 0

if (fields) hash.fields = fields
if (omitFields) hash.fields = omitFields
if (sort) hash.sort = sort

if (query.offset) hash.skip = Number(query.offset)
Expand All @@ -136,7 +149,7 @@ module.exports = function(query, options) {
} else {
options.ignore = (typeof options.ignore === 'string') ? [options.ignore] : options.ignore
}
options.ignore = options.ignore.concat(['fields', 'sort', 'skip', 'limit'])
options.ignore = options.ignore.concat(['fields', 'omit', 'sort', 'skip', 'limit'])
if (!options.parser) options.parser = querystring

if (typeof query === 'string') query = options.parser.parse(query)
Expand Down

0 comments on commit 1d091c4

Please sign in to comment.