Skip to content

Commit df029b8

Browse files
dstarkeArthur Cinader
authored and
Arthur Cinader
committed
LiveQuery should match subobjects with dot notation (#3322)
* LiveQuery should match subobjects with dot notation * one additional test case
1 parent edba550 commit df029b8

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

spec/QueryTools.spec.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,66 @@ describe('matchesQuery', function() {
412412
expect(matchesQuery(caltrainStation, q)).toBe(false);
413413
expect(matchesQuery(santaClara, q)).toBe(false);
414414
});
415+
416+
it('matches on subobjects with dot notation', function() {
417+
var message = {
418+
id: new Id('Message', 'O1'),
419+
text: "content",
420+
status: {x: "read", y: "delivered"}
421+
};
422+
423+
var q = new Parse.Query('Message');
424+
q.equalTo("status.x", "read");
425+
expect(matchesQuery(message, q)).toBe(true);
426+
427+
q = new Parse.Query('Message');
428+
q.equalTo("status.z", "read");
429+
expect(matchesQuery(message, q)).toBe(false);
430+
431+
q = new Parse.Query('Message');
432+
q.equalTo("status.x", "delivered");
433+
expect(matchesQuery(message, q)).toBe(false);
434+
435+
q = new Parse.Query('Message');
436+
q.notEqualTo("status.x", "read");
437+
expect(matchesQuery(message, q)).toBe(false);
438+
439+
q = new Parse.Query('Message');
440+
q.notEqualTo("status.z", "read");
441+
expect(matchesQuery(message, q)).toBe(true);
442+
443+
q = new Parse.Query('Message');
444+
q.notEqualTo("status.x", "delivered");
445+
expect(matchesQuery(message, q)).toBe(true);
446+
447+
q = new Parse.Query('Message');
448+
q.exists("status.x");
449+
expect(matchesQuery(message, q)).toBe(true);
450+
451+
q = new Parse.Query('Message');
452+
q.exists("status.z");
453+
expect(matchesQuery(message, q)).toBe(false);
454+
455+
q = new Parse.Query('Message');
456+
q.exists("nonexistent.x");
457+
expect(matchesQuery(message, q)).toBe(false);
458+
459+
q = new Parse.Query('Message');
460+
q.doesNotExist("status.x");
461+
expect(matchesQuery(message, q)).toBe(false);
462+
463+
q = new Parse.Query('Message');
464+
q.doesNotExist("status.z");
465+
expect(matchesQuery(message, q)).toBe(true);
466+
467+
q = new Parse.Query('Message');
468+
q.doesNotExist("nonexistent.z");
469+
expect(matchesQuery(message, q)).toBe(true);
470+
471+
q = new Parse.Query('Message');
472+
q.equalTo("status.x", "read");
473+
q.doesNotExist("status.y");
474+
expect(matchesQuery(message, q)).toBe(false);
475+
476+
});
415477
});

src/LiveQuery/QueryTools.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ function matchesKeyConstraints(object, key, constraints) {
133133
if (constraints === null) {
134134
return false;
135135
}
136+
if(key.indexOf(".") >= 0){
137+
// Key references a subobject
138+
var keyComponents = key.split(".");
139+
var subObjectKey = keyComponents[0];
140+
var keyRemainder = keyComponents.slice(1).join(".");
141+
return matchesKeyConstraints(object[subObjectKey] || {}, keyRemainder, constraints);
142+
}
136143
var i;
137144
if (key === '$or') {
138145
for (i = 0; i < constraints.length; i++) {

0 commit comments

Comments
 (0)