Skip to content

Commit 4fd73b0

Browse files
authored
Support containedBy Query (#633)
1 parent 506bc83 commit 4fd73b0

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

integration/test/ParseQueryTest.js

+47
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,53 @@ describe('Parse Query', () => {
192192
});
193193
});
194194

195+
it('can do containedBy queries with numbers', async () => {
196+
const NumberSet = Parse.Object.extend('NumberSet');
197+
const objectsList = [];
198+
objectsList.push(new NumberSet({ numbers: [0, 1, 2] }));
199+
objectsList.push(new NumberSet({ numbers: [2, 0] }));
200+
objectsList.push(new NumberSet({ numbers: [1, 2, 3, 4] }));
201+
202+
await Parse.Object.saveAll(objectsList);
203+
204+
const query = new Parse.Query(NumberSet);
205+
query.containedBy('numbers', [1, 2, 3, 4, 5]);
206+
const results = await query.find();
207+
assert.equal(results.length, 1);
208+
});
209+
210+
it('can do containedBy queries with pointer', async () => {
211+
const objects = Array.from(Array(10).keys()).map((idx) => {
212+
const obj = new Parse.Object('Object');
213+
obj.set('key', idx);
214+
return obj;
215+
});
216+
217+
const parent1 = new Parse.Object('Parent');
218+
const parent2 = new Parse.Object('Parent');
219+
const parent3 = new Parse.Object('Parent');
220+
221+
await Parse.Object.saveAll(objects);
222+
223+
// [0, 1, 2]
224+
parent1.set('objects', objects.slice(0, 3));
225+
226+
const shift = objects.shift();
227+
// [2, 0]
228+
parent2.set('objects', [objects[1], shift]);
229+
230+
// [1, 2, 3, 4]
231+
parent3.set('objects', objects.slice(1, 4));
232+
233+
await Parse.Object.saveAll([parent1, parent2, parent3]);
234+
const query = new Parse.Query('Parent');
235+
query.containedBy('objects', objects);
236+
const results = await query.find();
237+
238+
assert.equal(results.length, 1);
239+
assert.equal(results[0].id, parent3.id);
240+
});
241+
195242
it('can do equalTo queries', (done) => {
196243
let query = new Parse.Query('BoxedNumber');
197244
query.equalTo('number', 3);

src/ParseQuery.js

+11
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,17 @@ class ParseQuery {
831831
return this._addCondition(key, '$nin', value);
832832
}
833833

834+
/**
835+
* Adds a constraint to the query that requires a particular key's value to
836+
* be contained by the provided list of values. Get objects where all array elements match.
837+
* @param {String} key The key to check.
838+
* @param {Array} values The values that will match.
839+
* @return {Parse.Query} Returns the query, so you can chain this call.
840+
*/
841+
containedBy(key: string, value: Array<mixed>): ParseQuery {
842+
return this._addCondition(key, '$containedBy', value);
843+
}
844+
834845
/**
835846
* Adds a constraint to the query that requires a particular key's value to
836847
* contain each one of the provided list of values.

src/__tests__/ParseQuery-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,27 @@ describe('ParseQuery', () => {
263263
});
264264
});
265265

266+
it('can generate containedBy queries', () => {
267+
const q = new ParseQuery('Item');
268+
q.containedBy('tags', ['hot', 'sold-out']);
269+
expect(q.toJSON()).toEqual({
270+
where: {
271+
tags: {
272+
$containedBy: ['hot', 'sold-out']
273+
},
274+
},
275+
});
276+
277+
q.containedBy('tags', ['sale', 'new']);
278+
expect(q.toJSON()).toEqual({
279+
where: {
280+
tags: {
281+
$containedBy: ['sale', 'new']
282+
},
283+
},
284+
});
285+
});
286+
266287
it('can generate contains-all-starting-with queries', () => {
267288
var q = new ParseQuery('Item');
268289
q.containsAllStartingWith('tags', ['ho', 'out']);

0 commit comments

Comments
 (0)