Skip to content

Commit

Permalink
Merge dd3ce1a into a5ffb95
Browse files Browse the repository at this point in the history
  • Loading branch information
sadakchap authored Jan 6, 2022
2 parents a5ffb95 + dd3ce1a commit c4a2ef9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
65 changes: 65 additions & 0 deletions spec/QueryTools.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
26 changes: 26 additions & 0 deletions src/LiveQuery/QueryTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c4a2ef9

Please sign in to comment.