Added Local Mongo support for nested documents. Totally forgot those

This commit is contained in:
Ruben Vereecken 2014-05-17 17:19:57 +02:00
parent e177912a5a
commit 699aa506f2
2 changed files with 16 additions and 4 deletions

View file

@ -22,6 +22,7 @@ doQuerySelector = (value, operatorObj) ->
true
matchesQuery = (target, queryObj) ->
return true unless queryObj
for prop, query of queryObj
if prop[0] == '$'
switch prop
@ -29,10 +30,15 @@ matchesQuery = (target, queryObj) ->
when '$and' then return false unless _.reduce query, ((res, obj) -> res and matchesQuery target, obj), true
else return false
else
return false unless prop of target
# Do nested properties
pieces = prop.split('.')
obj = target
for piece in pieces
return false unless piece of obj
obj = obj[piece]
if typeof query != 'object' or _.isArray query
return false unless target[prop] == query or (query in target[prop] if _.isArray target[prop])
else return false unless doQuerySelector target[prop], query
return false unless obj == query or (query in obj if _.isArray obj)
else return false unless doQuerySelector obj, query
true
LocalMongo.matchesQuery = matchesQuery

View file

@ -10,6 +10,8 @@ describe 'Local Mongo queries', ->
'type': 'unicorn'
'likes': ['poptarts', 'popsicles', 'popcorn']
this.fixture2 = this: is: so: 'deep'
it 'regular match of a property', ->
expect(LocalMongo.matchesQuery(this.fixture1, 'gender': 'unicorn')).toBeFalsy()
expect(LocalMongo.matchesQuery(this.fixture1, 'type':'unicorn')).toBeTruthy()
@ -20,6 +22,9 @@ describe 'Local Mongo queries', ->
expect(LocalMongo.matchesQuery(this.fixture1, 'likes':'poptarts')).toBeTruthy()
expect(LocalMongo.matchesQuery(this.fixture1, 'likes':'walks on the beach')).toBeFalsy()
it 'nested match', ->
expect(LocalMongo.matchesQuery(this.fixture2, 'this.is.so':'deep')).toBeTruthy()
it '$gt selector', ->
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gt': 8000)).toBeTruthy()
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gt': [8000, 10000])).toBeTruthy()
@ -68,4 +73,5 @@ describe 'Local Mongo queries', ->
it '$and operator', ->
expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:9000}, {type:'zebra'}])).toBeFalsy()
expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:9000}, {type:'unicorn'}])).toBeTruthy()
expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:$gte:9000}, {worth:$lt:10}])).toBeTruthy()
expect(LocalMongo.matchesQuery(this.fixture1, $and: [{value:$gte:9000}, {worth:$lt:10}])).toBeTruthy()