diff --git a/spec/QueryTools.spec.js b/spec/QueryTools.spec.js index 7e09078dad..28bbc7a9e4 100644 --- a/spec/QueryTools.spec.js +++ b/spec/QueryTools.spec.js @@ -255,6 +255,71 @@ describe('matchesQuery', function () { expect(matchesQuery(img, q)).toBe(false); }); + it('matches on queries with new format #parse-SDK-JS/pull/1373', function () { + const obj1 = { + objectId: 'Person01', + score: 12, + name: 'Bill', + }; + + const q = { + score: { + $eq: 12, + }, + }; + expect(matchesQuery(obj1, q)).toBe(true); + }); + + it('matches on queries with new format #parse-SDK-JS/pull/1373 for Pointer', function () { + const obj1 = { + objectId: 'Person01', + name: 'Bill', + age: 34, + }; + + const obj2 = { + objectId: 'Car01', + name: 'Volkswagen', + owner: pointer('Person', obj1.objectId), + }; + + const q = { + owner: { + $eq: pointer('Person', obj1.objectId), + }, + }; + expect(matchesQuery(obj2, q)).toBe(true); + }); + + it('matches on queries with new format #parse-SDK-JS/pull/1373 for Object', function () { + const obj1 = { + objectId: 'Person01', + addr: { planet: 'Earth' }, + }; + + const q = { + addr: { + $eq: { planet: 'Earth' }, + }, + }; + expect(matchesQuery(obj1, q)).toBe(true); + }); + + it('matches on queries with new format #parse-SDK-JS/pull/1373 for Array', function () { + const obj = { + objectId: 'Person01', + name: 'Bill', + nums: [1, 2, 3, 4], + }; + + const q = { + nums: { + $eq: 5, + }, + }; + expect(matchesQuery(obj, q)).toBe(false); + }); + it('matches on inequalities', function () { const player = { id: new Id('Person', 'O1'), diff --git a/src/LiveQuery/QueryTools.js b/src/LiveQuery/QueryTools.js index 905919ef61..db9b4a0608 100644 --- a/src/LiveQuery/QueryTools.js +++ b/src/LiveQuery/QueryTools.js @@ -217,6 +217,32 @@ function matchesKeyConstraints(object, key, constraints) { compareTo = Parse._decode(key, compareTo); } switch (condition) { + case '$eq': { + if (typeof compareTo !== 'object') { + // Equality (or Array contains) cases + if (Array.isArray(object[key])) { + if (object[key].indexOf(constraints[condition]) === -1) { + return false; + } + break; + } + if (object[key] !== compareTo) { + return false; + } + break; + } else { + if (compareTo && constraints[condition].__type === 'Pointer') { + return equalObjectsGeneric(object[key], constraints[condition], function (obj, ptr) { + return ( + typeof obj !== 'undefined' && + ptr.className === obj.className && + ptr.objectId === obj.objectId + ); + }); + } + return equalObjectsGeneric(object[key], compareTo, equalObjects); + } + } case '$lt': if (object[key] >= compareTo) { return false;