Fixed ability to query tasks collection using task ID. Fixes #68.

This commit is contained in:
Zaid Abdulla committed 2015-05-26 12:54:04 +03:00
1 parent 4ad8aa5a1b
commit 305558f186
2 files changed
+27

No files matched your search

+1
View File
@@ -343,6 +343,7 @@ listener = (request, response) ->
else else
q = {} q = {}
q = query.expand(q, aliases) if collectionName is 'devices' q = query.expand(q, aliases) if collectionName is 'devices'
q = query.substituteObjectId(q) if collectionName is 'tasks'
if urlParts.query.projection? if urlParts.query.projection?
projection = {} projection = {}
+26
View File
@@ -22,6 +22,7 @@
common = require './common' common = require './common'
normalize = require './normalize' normalize = require './normalize'
mongodb = require 'mongodb'
expandValue = (param, value) -> expandValue = (param, value) ->
@@ -103,5 +104,30 @@ expand = (query, aliases) ->
return new_query return new_query
# Replace _id string values with ObjectID type
substituteObjectId = (query) ->
for k, v of query
if k[0] == '$' # logical operator
for i in [0...v.length]
substituteObjectId(v[i])
else if k == '_id'
if common.typeOf(v) is common.STRING_TYPE
query[k] = mongodb.ObjectID(v) if v.length == 24
else if common.typeOf(v) is common.OBJECT_TYPE
for kk, vv of v
switch kk # operator
when '$in', '$nin'
for i in [0...vv.length]
vv[i] = mongodb.ObjectID(vv[i]) if vv[i].length == 24
when '$eq', '$gt', '$gte', '$lt', '$lte', '$ne'
v[kk] = mongodb.ObjectID(vv) if vv.length == 24
when '$exists', '$type' then
# ignore
else
throw new Error('Operator not supported')
return query
exports.expand = expand exports.expand = expand
exports.substituteObjectId = substituteObjectId