Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for $eq in LiveQuery #7607

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () {
Copy link
Member

@mtrezza mtrezza Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related to "add support for $eq in LiveQuery"? This is not a LiveQuery test. Is this a test for parse-community/Parse-SDK-JS#1373 which is a separate issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since in parse-community/Parse-SDK-JS#1373, we are changing the format of query. There are certain LiveQuery tests(parse-sdk-js) which are failing because parse-server doesn't support new query format for LiveQuery.

So, we added support for new query format in LiveQuery in parse-server.

Copy link
Member

@mtrezza mtrezza Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow; is parse-community/Parse-SDK-JS#1373 a requirement for this PR? I don't think so, otherwise the tests in this PR wouldn't already pass.

Copy link
Member Author

@sadakchap sadakchap Jan 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually its the other, please follow this parse-community/Parse-SDK-JS#1373 (comment)

Copy link
Member

@mtrezza mtrezza Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I understand that the tests in parse-community/Parse-SDK-JS#1373 will only pass when this PR (#7607) has been merged. But they relate to 2 different features:

  • add support for $eq in LiveQuery
  • add support to "combine equalTo clauses with [other] clauses"

So why is parse-community/Parse-SDK-JS#1373 mentioned in this test case if this PR (#7607) has no dependency on parse-community/Parse-SDK-JS#1373, only the other way around?

Copy link
Member Author

@sadakchap sadakchap Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtrezza sorry, I could not understand you properly, could you please elaborate a bit more?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it seems these are 2 different features:

Even if parse-community/Parse-SDK-JS#1373 never gets merged, we would still want to merge #7607 as a feature, right? So I don't understand why the tests in #7607 mention parse-community/Parse-SDK-JS#1373:

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