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: combine equalTo clauses with clauses #1373

Open
wants to merge 13 commits into
base: alpha
Choose a base branch
from
Open
41 changes: 38 additions & 3 deletions src/OfflineQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,11 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
for (const condition in constraints) {
compareTo = constraints[condition];

if (compareTo.__type) {
if (compareTo && compareTo.__type) {
compareTo = decode(compareTo);
}
// is it a $relativeTime? convert to date
if (compareTo['$relativeTime']) {
if (compareTo && compareTo['$relativeTime']) {
const parserResult = relativeTimeToDate(compareTo['$relativeTime']);
if (parserResult.status !== 'success') {
throw new ParseError(
Expand All @@ -327,12 +327,47 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
toString.call(compareTo) === '[object Date]' ||
(typeof compareTo === 'string' &&
new Date(compareTo) !== 'Invalid Date' &&
!isNaN(new Date(compareTo)))
!isNaN(new Date(compareTo)) &&
new Date(compareTo).toISOString() === compareTo)
) {
object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
}

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') {
if (
!equalObjectsGeneric(object[key], constraints[condition], function (obj, ptr) {
return (
typeof obj !== 'undefined' &&
ptr.className === obj.className &&
ptr.objectId === obj.objectId
);
})
) {
return false;
}
break;
}
if (!equalObjects(decode(object[key]), compareTo)) {
return false;
}
}
break;
}
case '$lt':
if (object[key] >= compareTo) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,8 @@ class ParseQuery {
return this.doesNotExist(key);
}

this._where[key] = encode(value, false, true);
// this._where[key] = encode(value, false, true);
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems to be a leftover?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, will remove.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will update the pr once all checks are passing.

this._addCondition(key, '$eq', value);
return this;
}

Expand Down
12 changes: 9 additions & 3 deletions src/__tests__/Cloud-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ describe('CloudController', () => {
expect(data).toEqual({
limit: 1,
where: {
objectId: 'jobId1234',
Copy link
Member

Choose a reason for hiding this comment

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

Should all these tests be modified? That means the original syntax would not be tested anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, all test are modified. And yes, original syntax would not be tested anymore as it will always make any equalTo query with this updated syntax { $eq: '' }

Copy link
Member

Choose a reason for hiding this comment

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

Could that be a breaking change for existing deployments?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see it as a breaking change, as it will just change the syntax of query for equalTo, everything else should be same.

For adding equalTo query, we still would be doing same thing.

q.equalTo('age', 10); // same implementation, just query that will be sent is of new syntax.

Copy link
Member

Choose a reason for hiding this comment

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

Does this mean the query sent to the DB has a different syntax, or only the query sent to Parse Server?

objectId: {
$eq: 'jobId1234',
},
},
});
expect(options.useMasterKey).toBe(true);
Expand All @@ -323,7 +325,9 @@ describe('CloudController', () => {
expect(data).toEqual({
limit: 1,
where: {
objectId: 'pushId1234',
objectId: {
$eq: 'pushId1234',
},
},
});
expect(options.useMasterKey).toBe(true);
Expand All @@ -345,7 +349,9 @@ describe('CloudController', () => {
expect(data).toEqual({
limit: 1,
where: {
objectId: 'pushId1234',
objectId: {
$eq: 'pushId1234',
},
},
});
expect(options.useMasterKey).toBe(false);
Expand Down
16 changes: 12 additions & 4 deletions src/__tests__/LiveQueryClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,9 @@ describe('LiveQueryClient', () => {
query: {
className: 'Test',
where: {
key: 'value',
key: {
$eq: 'value',
},
},
},
});
Expand Down Expand Up @@ -842,7 +844,9 @@ describe('LiveQueryClient', () => {
query: {
className: 'Test',
where: {
key: 'value',
key: {
$eq: 'value',
},
},
},
});
Expand Down Expand Up @@ -921,7 +925,9 @@ describe('LiveQueryClient', () => {
query: {
className: 'Test',
where: {
key: 'value',
key: {
$eq: 'value',
},
},
},
});
Expand Down Expand Up @@ -956,7 +962,9 @@ describe('LiveQueryClient', () => {
query: {
className: 'Test',
where: {
key: 'value',
key: {
$eq: 'value',
},
},
},
});
Expand Down