Skip to content

Adds support for pointer/string pointers comparison in LiveQuery #4231

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

Merged
merged 5 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
75 changes: 75 additions & 0 deletions spec/QueryTools.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,79 @@ describe('matchesQuery', function() {
expect(matchesQuery(message, q)).toBe(false);

});

function pointer(className, objectId) {
return { __type: 'Pointer', className, objectId };
}

it('should support containedIn with pointers', () => {
var message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'abc')
};
var q = new Parse.Query('Message');
q.containedIn('profile', [Parse.Object.fromJSON({ className: 'Profile', objectId: 'abc' }),
Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' })]);
expect(matchesQuery(message, q)).toBe(true);

q = new Parse.Query('Message');
q.containedIn('profile', [Parse.Object.fromJSON({ className: 'Profile', objectId: 'ghi' }),
Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' })]);
expect(matchesQuery(message, q)).toBe(false);
});

it('should support notContainedIn with pointers', () => {
var message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'abc')
};
var q = new Parse.Query('Message');
q.notContainedIn('profile', [Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' }),
Parse.Object.fromJSON({ className: 'Profile', objectId: 'ghi' })]);
expect(matchesQuery(message, q)).toBe(true);

message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'def')
};
q = new Parse.Query('Message');
q.notContainedIn('profile', [Parse.Object.fromJSON({ className: 'Profile', objectId: 'ghi' }),
Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' })]);
expect(matchesQuery(message, q)).toBe(false);
});

it('should support containedIn queries with [objectId]', () => {
var message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'abc')
};
var q = new Parse.Query('Message');
q.containedIn('profile', ['abc', 'def']);
expect(matchesQuery(message, q)).toBe(true);

message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'ghi')
};
q = new Parse.Query('Message');
q.containedIn('profile', ['abc', 'def']);
expect(matchesQuery(message, q)).toBe(false);
});

it('should support notContainedIn queries with [objectId]', () => {
var message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'ghi')
};
var q = new Parse.Query('Message');
q.notContainedIn('profile', ['abc', 'def']);
expect(matchesQuery(message, q)).toBe(true);
message = {
id: new Id('Message', 'O1'),
profile: pointer('Profile', 'ghi')
};
q = new Parse.Query('Message');
q.notContainedIn('profile', ['abc', 'def', 'ghi']);
expect(matchesQuery(message, q)).toBe(false);
});
});
28 changes: 26 additions & 2 deletions src/LiveQuery/QueryTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ function queryHash(query) {
return query.className + ':' + sections.join('|');
}

/**
* contains -- Determines whether a constraint in the form of a list
Copy link
Contributor

Choose a reason for hiding this comment

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

how about:

Determines if an object is contained in a list with special handling for Parse pointers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LOVE it!

* contains a particular object.
* If the compared to object is a pointer it will match either full pointers
* or string (objectId)
* Otherwise it will use a simple indexOf.
*/
function contains(haystack: Array, needle: any): boolean {
if (needle && needle.__type && needle.__type === 'Pointer') {
for (const i in haystack) {
const ptr = haystack[i];
if (typeof ptr === 'string' && ptr === needle.objectId) {
return true;
}
if (typeof object !== 'undefined' &&
ptr.className === needle.className &&
ptr.objectId === needle.objectId) {
return true;
}
}
return false;
}
return haystack.indexOf(needle) > -1;
}
/**
* matchesQuery -- Determines if an object would be returned by a Parse Query
* It's a lightweight, where-clause only implementation of a full query engine.
Expand Down Expand Up @@ -207,12 +231,12 @@ function matchesKeyConstraints(object, key, constraints) {
}
break;
case '$in':
if (compareTo.indexOf(object[key]) < 0) {
if (!contains(compareTo, object[key])) {
return false;
}
break;
case '$nin':
if (compareTo.indexOf(object[key]) > -1) {
if (contains(compareTo, object[key])) {
return false;
}
break;
Expand Down