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

This commit is contained in:
Zaid Abdulla committed 2015-05-26 12:58:12 +03:00
1 parent 7c8036d8f3
commit 6f595b6fc5
2 files changed
+27

No files matched your search

+26
View File
@@ -22,6 +22,7 @@
common = require './common'
normalize = require './normalize'
mongodb = require 'mongodb'
expandValue = (param, value) ->
@@ -103,5 +104,30 @@ expand = (query, aliases) ->
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.substituteObjectId = substituteObjectId