Skip to content

Commit 3070515

Browse files
authored
feat(query/equalTo + notEqualTo): Add possibility to pass objects (#1235)
* equalTo + notEqualTo: Add possibility to pass objects * remove console.log * Add integration tests
1 parent c922a7c commit 3070515

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

integration/test/ParseQueryTest.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ describe('Parse Query', () => {
362362
});
363363
});
364364

365+
it('can do equalTo queries with object', (done) => {
366+
const query = new Parse.Query('BoxedNumber');
367+
query.equalTo({ number: 3 });
368+
query.find().then((results) => {
369+
assert.equal(results.length, 1);
370+
done();
371+
});
372+
});
373+
365374
it('can test equality with undefined', (done) => {
366375
const query = new Parse.Query('BoxedNumber');
367376
query.equalTo('number', undefined);
@@ -436,6 +445,15 @@ describe('Parse Query', () => {
436445
});
437446
});
438447

448+
it('can perform notEqualTo queries with object', (done) => {
449+
const query = new Parse.Query('BoxedNumber');
450+
query.notEqualTo({ number: 5 });
451+
query.find().then((results) => {
452+
assert.equal(results.length, 9);
453+
done();
454+
});
455+
});
456+
439457
it('can perform containedIn queries', (done) => {
440458
const query = new Parse.Query('BoxedNumber');
441459
query.containedIn('number', [3,5,7,9,11]);

src/ParseQuery.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,11 @@ class ParseQuery {
11691169
* @param value The value that the Parse.Object must contain.
11701170
* @return {Parse.Query} Returns the query, so you can chain this call.
11711171
*/
1172-
equalTo(key: string, value: mixed): ParseQuery {
1172+
equalTo(key: string | { [key: string]: any }, value: ?mixed): ParseQuery {
1173+
if (key && typeof key === 'object') {
1174+
Object.entries(key).forEach(([k, val]) => this.equalTo(k, val))
1175+
return this
1176+
}
11731177
if (typeof value === 'undefined') {
11741178
return this.doesNotExist(key);
11751179
}
@@ -1185,7 +1189,11 @@ class ParseQuery {
11851189
* @param value The value that must not be equalled.
11861190
* @return {Parse.Query} Returns the query, so you can chain this call.
11871191
*/
1188-
notEqualTo(key: string, value: mixed): ParseQuery {
1192+
notEqualTo(key: string | { [key: string]: any }, value: ?mixed): ParseQuery {
1193+
if (key && typeof key === 'object') {
1194+
Object.entries(key).forEach(([k, val]) => this.notEqualTo(k, val))
1195+
return this
1196+
}
11891197
return this._addCondition(key, '$ne', value);
11901198
}
11911199

src/__tests__/ParseQuery-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ describe('ParseQuery', () => {
109109
}
110110
}
111111
});
112+
113+
const size = 'medium';
114+
const stock = true;
115+
q.equalTo({ size, stock });
116+
expect(q.toJSON()).toEqual({
117+
where: {
118+
size: 'medium',
119+
stock: true,
120+
}
121+
});
112122
});
113123

114124
it('can generate inequality queries', () => {
@@ -130,6 +140,20 @@ describe('ParseQuery', () => {
130140
}
131141
}
132142
});
143+
144+
const size = 'medium';
145+
const stock = true;
146+
q.notEqualTo({ size, stock });
147+
expect(q.toJSON()).toEqual({
148+
where: {
149+
size: {
150+
$ne: 'medium',
151+
},
152+
stock: {
153+
$ne: true,
154+
}
155+
}
156+
});
133157
});
134158

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

0 commit comments

Comments
 (0)