Skip to content

Support nor Query #634

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
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
49 changes: 49 additions & 0 deletions integration/test/ParseQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,55 @@ describe('Parse Query', () => {
}).catch(e => console.log(e));
});

it('can build NOR queries', async () => {
const objects = [];
for (let i = 0; i < 10; i += 1) {
const obj = new Parse.Object('NORTest');
obj.set({ x: i });
objects.push(obj);
}
await Parse.Object.saveAll(objects);

const q1 = new Parse.Query('NORTest');
q1.greaterThan('x', 5);
const q2 = new Parse.Query('NORTest');
q2.lessThan('x', 3);
const norQuery = Parse.Query.nor(q1, q2);
const results = await norQuery.find();

assert.equal(results.length, 3);
results.forEach((number) => {
assert(number.get('x') >= 3 && number.get('x') <= 5);
});
});

it('can build complex NOR queries', async () => {
const objects = [];
for (let i = 0; i < 10; i += 1) {
const child = new Parse.Object('Child');
child.set('x', i);
const parent = new Parse.Object('Parent');
parent.set('child', child);
parent.set('y', i);
objects.push(parent);
}
await Parse.Object.saveAll(objects);

const subQuery = new Parse.Query('Child');
subQuery.equalTo('x', 4);
const q1 = new Parse.Query('Parent');
q1.matchesQuery('child', subQuery);
const q2 = new Parse.Query('Parent');
q2.equalTo('y', 5);
const norQuery = new Parse.Query.nor(q1, q2);
const results = await norQuery.find();

assert.equal(results.length, 8);
results.forEach((number) => {
assert(number.get('x') !== 4 || number.get('x') !== 5);
});
});

it('can iterate over results with each', (done) => {
let items = [];
for (let i = 0; i < 50; i++) {
Expand Down
33 changes: 32 additions & 1 deletion src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ class ParseQuery {

/**
* Adds constraint that all of the passed in queries match.
* @method _andQuery
* @param {Array} queries
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
Expand All @@ -250,6 +249,20 @@ class ParseQuery {
return this;
}

/**
* Adds constraint that none of the passed in queries match.
* @param {Array} queries
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
_norQuery(queries: Array<ParseQuery>): ParseQuery {
var queryJSON = queries.map((q) => {
return q.toJSON().where;
});

this._where.$nor = queryJSON;
return this;
}

/**
* Helper for condition queries
*/
Expand Down Expand Up @@ -1422,6 +1435,24 @@ class ParseQuery {
query._andQuery(queries);
return query;
}

/**
* Constructs a Parse.Query that is the NOR of the passed in queries. For
* example:
* <pre>const compoundQuery = Parse.Query.nor(query1, query2, query3);</pre>
*
* will create a compoundQuery that is a nor of the query1, query2, and
* query3.
* @param {...Parse.Query} var_args The list of queries to NOR.
* @static
* @return {Parse.Query} The query that is the NOR of the passed in queries.
*/
static nor(...queries: Array<ParseQuery>): ParseQuery {
const className = _getClassNameFromQueries(queries);
const query = new ParseQuery(className);
query._norQuery(queries);
return query;
}
}

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

it('can combine queries with a NOR clause', () => {
const q = new ParseQuery('Item');
let q2 = new ParseQuery('Purchase');
expect(ParseQuery.nor.bind(null, q, q2)).toThrow(
'All queries must be for the same class.',
);

q2 = new ParseQuery('Item');
q.equalTo('size', 'medium');
q2.equalTo('size', 'large');

let mediumOrLarge = ParseQuery.nor(q, q2);
expect(mediumOrLarge.toJSON()).toEqual({
where: {
$nor: [
{ size: 'medium' },
{ size: 'large' },
],
},
});

// It removes limits, skips, etc
q.limit(10);
mediumOrLarge = ParseQuery.nor(q, q2);
expect(mediumOrLarge.toJSON()).toEqual({
where: {
$nor: [
{ size: 'medium' },
{ size: 'large' },
],
},
});
});

it('can get the first object of a query', (done) => {
CoreManager.setQueryController({
aggregate() {},
Expand Down