Skip to content

equalTo + notEqualTo: Add possibility to pass objects #1235

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 3 commits into from
Oct 22, 2020
Merged
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
18 changes: 18 additions & 0 deletions integration/test/ParseQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ describe('Parse Query', () => {
});
});

it('can do equalTo queries with object', (done) => {
const query = new Parse.Query('BoxedNumber');
query.equalTo({ number: 3 });
query.find().then((results) => {
assert.equal(results.length, 1);
done();
});
});

it('can test equality with undefined', (done) => {
const query = new Parse.Query('BoxedNumber');
query.equalTo('number', undefined);
Expand Down Expand Up @@ -436,6 +445,15 @@ describe('Parse Query', () => {
});
});

it('can perform notEqualTo queries with object', (done) => {
const query = new Parse.Query('BoxedNumber');
query.notEqualTo({ number: 5 });
query.find().then((results) => {
assert.equal(results.length, 9);
done();
});
});

it('can perform containedIn queries', (done) => {
const query = new Parse.Query('BoxedNumber');
query.containedIn('number', [3,5,7,9,11]);
Expand Down
12 changes: 10 additions & 2 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,11 @@ class ParseQuery {
* @param value The value that the Parse.Object must contain.
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
equalTo(key: string, value: mixed): ParseQuery {
equalTo(key: string | { [key: string]: any }, value: ?mixed): ParseQuery {
if (key && typeof key === 'object') {
Object.entries(key).forEach(([k, val]) => this.equalTo(k, val))
return this
}
if (typeof value === 'undefined') {
return this.doesNotExist(key);
}
Expand All @@ -1185,7 +1189,11 @@ class ParseQuery {
* @param value The value that must not be equalled.
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
notEqualTo(key: string, value: mixed): ParseQuery {
notEqualTo(key: string | { [key: string]: any }, value: ?mixed): ParseQuery {
if (key && typeof key === 'object') {
Object.entries(key).forEach(([k, val]) => this.notEqualTo(k, val))
return this
}
return this._addCondition(key, '$ne', value);
}

Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ describe('ParseQuery', () => {
}
}
});

const size = 'medium';
const stock = true;
q.equalTo({ size, stock });
expect(q.toJSON()).toEqual({
where: {
size: 'medium',
stock: true,
}
});
});

it('can generate inequality queries', () => {
Expand All @@ -130,6 +140,20 @@ describe('ParseQuery', () => {
}
}
});

const size = 'medium';
const stock = true;
q.notEqualTo({ size, stock });
expect(q.toJSON()).toEqual({
where: {
size: {
$ne: 'medium',
},
stock: {
$ne: true,
}
}
});
});

it('can generate less-than queries', () => {
Expand Down