Skip to content

Support includeAll Query #632

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

it('can includeAll nested objects', async () => {
const child1 = new TestObject({ foo: 'bar' });
const child2 = new TestObject({ foo: 'baz' });
const child3 = new TestObject({ foo: 'bin' });
const parent = new Parse.Object('Container');
parent.set('child1', child1);
parent.set('child2', child2);
parent.set('child3', child3);
await Parse.Object.saveAll([child1, child2, child3, parent]);

const query = new Parse.Query('Container');
query.equalTo('objectId', parent.id);
query.includeAll();

const results = await query.find();

assert.equal(results.length, 1);
const parentAgain = results[0];
assert.equal(parentAgain.get('child1').get('foo'), 'bar');
assert.equal(parentAgain.get('child2').get('foo'), 'baz');
assert.equal(parentAgain.get('child3').get('foo'), 'bin');
});

it('can includeAll nested objects in .each', async () => {
const child1 = new TestObject({ foo: 'bar' });
const child2 = new TestObject({ foo: 'baz' });
const child3 = new TestObject({ foo: 'bin' });
const parent = new Parse.Object('Container');
parent.set('child1', child1);
parent.set('child2', child2);
parent.set('child3', child3);
await Parse.Object.saveAll([child1, child2, child3, parent]);

const query = new Parse.Query('Container');
query.equalTo('objectId', parent.id);
query.includeAll();

await query.each((obj) => {
assert.equal(obj.get('child1').get('foo'), 'bar');
assert.equal(obj.get('child2').get('foo'), 'baz');
assert.equal(obj.get('child3').get('foo'), 'bin');
});
});

it('can include nested objects via array', (done) => {
let child = new TestObject();
let parent = new Parse.Object('Container');
Expand Down
24 changes: 21 additions & 3 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,11 @@ class ParseQuery {
this._order = json.order.split(",");
}

for (let key in json) if (json.hasOwnProperty(key)) {
if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
this._extraOptions[key] = json[key];
for (let key in json) {
if (json.hasOwnProperty(key)) {
if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
this._extraOptions[key] = json[key];
}
}
}

Expand Down Expand Up @@ -1313,6 +1315,11 @@ class ParseQuery {
/**
* Includes nested Parse.Objects for the provided key. You can use dot
* notation to specify which fields in the included object are also fetched.
*
* You can include all nested Parse.Objects by passing in '*'.
* Requires Parse Server 3.0.0+
* <pre>query.include('*');</pre>
*
* @param {...String|Array<String>} key The name(s) of the key(s) to include.
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
Expand All @@ -1327,6 +1334,17 @@ class ParseQuery {
return this;
}

/**
* Includes all nested Parse.Objects.
*
* Requires Parse Server 3.0.0+
*
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
includeAll(): ParseQuery {
return this.include('*');
}

/**
* Restricts the fields of the returned Parse.Objects to include only the
* provided keys. If this is called multiple times, then all of the keys
Expand Down
28 changes: 28 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,32 @@ describe('ParseQuery', () => {
});
});

it('can includeAll for pointers', () => {
const q = new ParseQuery('Item');
q.includeAll();
const json = q.toJSON();
expect(json).toEqual({
where: {},
include: '*',
});
const q2 = new ParseQuery('Item');
q2.withJSON(json);
expect(q2._include).toEqual(['*']);
});

it('can use extraOptions', () => {
const q = new ParseQuery('Item');
q._extraOptions.randomOption = 'test';
const json = q.toJSON();
expect(json).toEqual({
where: {},
randomOption: 'test',
});
const q2 = new ParseQuery('Item');
q2.withJSON(json);
expect(q2._extraOptions.randomOption).toBe('test');
});

it('can specify certain fields to send back', () => {
var q = new ParseQuery('Item');
q.select('size');
Expand Down Expand Up @@ -1300,6 +1326,7 @@ describe('ParseQuery', () => {
limit: 100,
order: 'objectId',
keys: 'size,name',
include: '*',
where: {
size: {
$in: ['small', 'medium']
Expand Down Expand Up @@ -1338,6 +1365,7 @@ describe('ParseQuery', () => {
);
q.equalTo('valid', true);
q.select('size', 'name');
q.includeAll();
var calls = 0;

q.each((o) => {
Expand Down