Commit 48a2d9a2 by Jose Carlos López

Add criteria

parent 3e4f68b0
Showing with 21 additions and 7 deletions
...@@ -142,8 +142,9 @@ function hasOrdinalKeys(obj) { ...@@ -142,8 +142,9 @@ function hasOrdinalKeys(obj) {
// Convert query parameters to a mongo query criteria. // Convert query parameters to a mongo query criteria.
// for example {field1:"red","field2>2":""} becomes {field1:"red",field2:{$gt:2}} // for example {field1:"red","field2>2":""} becomes {field1:"red",field2:{$gt:2}}
function queryCriteriaToMongo(query, options) { function queryCriteriaToMongo(query, options) {
var hash = {}, p, v, deep var options = options || {}
options = options || {} var p, v, deep
var hash = query.isOr ? [] : {}
for (var key in query) { for (var key in query) {
if (Object.prototype.hasOwnProperty.call(query,key) && (!options.ignore || options.ignore.indexOf(key) == -1)) { if (Object.prototype.hasOwnProperty.call(query,key) && (!options.ignore || options.ignore.indexOf(key) == -1)) {
deep = (typeof query[key] === 'object' && !hasOrdinalKeys(query[key])) deep = (typeof query[key] === 'object' && !hasOrdinalKeys(query[key]))
...@@ -158,7 +159,13 @@ function queryCriteriaToMongo(query, options) { ...@@ -158,7 +159,13 @@ function queryCriteriaToMongo(query, options) {
} }
if (p) { if (p) {
hash[p.key] = p.value if (query.isOr) {
var orQuery = {};
orQuery[p.key] = p.value
hash.push(orQuery)
} else {
hash[p.key] = p.value
}
} }
} }
} }
...@@ -201,13 +208,15 @@ module.exports = function(query, options) { ...@@ -201,13 +208,15 @@ module.exports = function(query, options) {
} else { } else {
options.ignore = (typeof options.ignore === 'string') ? [options.ignore] : options.ignore options.ignore = (typeof options.ignore === 'string') ? [options.ignore] : options.ignore
} }
options.ignore = options.ignore.concat(['fields', 'omit', 'sort', 'offset', 'limit']) options.ignore = options.ignore.concat(['fields', 'omit', 'sort', 'offset', 'limit', 'isOr'])
if (!options.parser) options.parser = querystring if (!options.parser) options.parser = querystring
if (typeof query === 'string') query = options.parser.parse(query) if (typeof query === 'string') query = options.parser.parse(query)
var criteria = queryCriteriaToMongo(query, options);
if (query.isOr) {
criteria = { $or: criteria};
}
return { return {
criteria: queryCriteriaToMongo(query, options), criteria: criteria,
options: queryOptionsToMongo(query, options), options: queryOptionsToMongo(query, options),
links: function(url, totalCount) { links: function(url, totalCount) {
......
...@@ -156,6 +156,11 @@ describe("query-to-mongo(query) =>", function () { ...@@ -156,6 +156,11 @@ describe("query-to-mongo(query) =>", function () {
assert.ok(results.criteria) assert.ok(results.criteria)
assert.deepEqual(results.criteria, {d: ObjectID('574553013b58d8582cdb1d5f')}) assert.deepEqual(results.criteria, {d: ObjectID('574553013b58d8582cdb1d5f')})
}) })
it("should create or criteria", function () {
var results = q2m("isOr=true&a>3&a<8")
assert.ok(results.criteria)
assert.deepEqual(results.criteria, { $or: [{ a: { $gt: 3 }}, { a: { $lt: 8 } }]})
})
}) })
describe(".options", function () { describe(".options", function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment