Skip to content

Support containedBy Query #633

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 2 commits into from
Aug 12, 2018
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
47 changes: 47 additions & 0 deletions integration/test/ParseQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,53 @@ describe('Parse Query', () => {
});
});

it('can do containedBy queries with numbers', async () => {
const NumberSet = Parse.Object.extend('NumberSet');
const objectsList = [];
objectsList.push(new NumberSet({ numbers: [0, 1, 2] }));
objectsList.push(new NumberSet({ numbers: [2, 0] }));
objectsList.push(new NumberSet({ numbers: [1, 2, 3, 4] }));

await Parse.Object.saveAll(objectsList);

const query = new Parse.Query(NumberSet);
query.containedBy('numbers', [1, 2, 3, 4, 5]);
const results = await query.find();
assert.equal(results.length, 1);
});

it('can do containedBy queries with pointer', async () => {
const objects = Array.from(Array(10).keys()).map((idx) => {
const obj = new Parse.Object('Object');
obj.set('key', idx);
return obj;
});

const parent1 = new Parse.Object('Parent');
const parent2 = new Parse.Object('Parent');
const parent3 = new Parse.Object('Parent');

await Parse.Object.saveAll(objects);

// [0, 1, 2]
parent1.set('objects', objects.slice(0, 3));

const shift = objects.shift();
// [2, 0]
parent2.set('objects', [objects[1], shift]);

// [1, 2, 3, 4]
parent3.set('objects', objects.slice(1, 4));

await Parse.Object.saveAll([parent1, parent2, parent3]);
const query = new Parse.Query('Parent');
query.containedBy('objects', objects);
const results = await query.find();

assert.equal(results.length, 1);
assert.equal(results[0].id, parent3.id);
});

it('can do equalTo queries', (done) => {
let query = new Parse.Query('BoxedNumber');
query.equalTo('number', 3);
Expand Down
11 changes: 11 additions & 0 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,17 @@ class ParseQuery {
return this._addCondition(key, '$nin', value);
}

/**
* Adds a constraint to the query that requires a particular key's value to
* be contained by the provided list of values. Get objects where all array elements match.
* @param {String} key The key to check.
* @param {Array} values The values that will match.
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
containedBy(key: string, value: Array<mixed>): ParseQuery {
return this._addCondition(key, '$containedBy', value);
}

/**
* Adds a constraint to the query that requires a particular key's value to
* contain each one of the provided list of values.
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ describe('ParseQuery', () => {
});
});

it('can generate containedBy queries', () => {
const q = new ParseQuery('Item');
q.containedBy('tags', ['hot', 'sold-out']);
expect(q.toJSON()).toEqual({
where: {
tags: {
$containedBy: ['hot', 'sold-out']
},
},
});

q.containedBy('tags', ['sale', 'new']);
expect(q.toJSON()).toEqual({
where: {
tags: {
$containedBy: ['sale', 'new']
},
},
});
});

it('can generate contains-all-starting-with queries', () => {
var q = new ParseQuery('Item');
q.containsAllStartingWith('tags', ['ho', 'out']);
Expand Down