Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 34 additions & 3 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class ParseQuery {
className: string;
_where: any;
_include: Array<string>;
_includeAll: boolean;
_select: Array<string>;
_limit: number;
_skip: number;
Expand Down Expand Up @@ -216,6 +217,7 @@ class ParseQuery {

this._where = {};
this._include = [];
this._includeAll = false;
this._limit = -1; // negative limit is not sent in the server request
this._skip = 0;
this._extraOptions = {};
Expand Down Expand Up @@ -280,6 +282,9 @@ class ParseQuery {
if (this._include.length) {
params.include = this._include.join(',');
}
if (this._includeAll) {
params.includeAll = true;
}
if (this._select) {
params.keys = this._select.join(',');
}
Expand Down Expand Up @@ -329,6 +334,10 @@ class ParseQuery {
this._include = json.include.split(",");
}

if (json.includeAll) {
this._includeAll = true;
}

if (json.keys) {
this._select = json.keys.split(",");
}
Expand All @@ -345,9 +354,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", "includeAll", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
this._extraOptions[key] = json[key];
}
}
}

Expand Down Expand Up @@ -677,6 +688,9 @@ class ParseQuery {
query._include = this._include.map((i) => {
return i;
});
if (this._includeAll) {
query._includeAll = true;
}
if (this._select) {
query._select = this._select.map((s) => {
return s;
Expand Down Expand Up @@ -1313,6 +1327,10 @@ 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.
*
* If you want to include all nested Parse.Objects pass in '*'
* <pre>query.include('*');</pre>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we need to put a version number here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which version? 2.1.0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean server version?

Copy link
Member Author

@dplewis dplewis Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea mate which version is, I can try to backtrack

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ok, so basically it already works if you pass include All in the rest SDK.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is correct. Basically this won't work until the next release unless I re-add includeAll to this SDK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let’s just use the * notation then we’ll perhaps be able to expand on with reg ex etc...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How best can we update the guides simultaneously with the API docs

*
* @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 @@ -1324,9 +1342,22 @@ class ParseQuery {
this._include.push(key);
}
});
if (this._include.includes('*')) {
this._includeAll = true;
this._include.splice(this._include.indexOf('*'), 1);
}
return this;
}

/**
* Includes all nested Parse.Objects.
*
* @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: {},
includeAll: true,
});
const q2 = new ParseQuery('Item');
q2.withJSON(json);
expect(q2._includeAll).toBe(true);
});

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',
includeAll: true,
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