From 1d66841f901f3ab3e6036df6d096ebcd69bddcff Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 10 Aug 2018 00:10:04 -0500 Subject: [PATCH 1/4] includeAll --- integration/test/ParseQueryTest.js | 44 ++++++++++++++++++++++++++++++ src/ParseQuery.js | 30 ++++++++++++++++++-- src/__tests__/ParseQuery-test.js | 28 +++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/integration/test/ParseQueryTest.js b/integration/test/ParseQueryTest.js index 20c6ae81b..763021000 100644 --- a/integration/test/ParseQueryTest.js +++ b/integration/test/ParseQueryTest.js @@ -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'); diff --git a/src/ParseQuery.js b/src/ParseQuery.js index 91d0b6b75..e4d36e697 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -183,6 +183,7 @@ class ParseQuery { className: string; _where: any; _include: Array; + _includeAll: boolean; _select: Array; _limit: number; _skip: number; @@ -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 = {}; @@ -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(','); } @@ -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(","); } @@ -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]; + } } } @@ -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; @@ -1327,6 +1341,16 @@ class ParseQuery { return this; } + /** + * Includes all nested Parse.Objects. + * + * @return {Parse.Query} Returns the query, so you can chain this call. + */ + includeAll(): ParseQuery { + this._includeAll = true; + return this; + } + /** * 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 diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js index 097d47191..f38123262 100644 --- a/src/__tests__/ParseQuery-test.js +++ b/src/__tests__/ParseQuery-test.js @@ -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'); @@ -1300,6 +1326,7 @@ describe('ParseQuery', () => { limit: 100, order: 'objectId', keys: 'size,name', + includeAll: true, where: { size: { $in: ['small', 'medium'] @@ -1338,6 +1365,7 @@ describe('ParseQuery', () => { ); q.equalTo('valid', true); q.select('size', 'name'); + q.includeAll(); var calls = 0; q.each((o) => { From eec2a0a0a19b54445b13088b2ba2b96c589d9def Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 10 Aug 2018 10:06:59 -0500 Subject: [PATCH 2/4] added * to include --- src/ParseQuery.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ParseQuery.js b/src/ParseQuery.js index e4d36e697..384fd2f72 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -1327,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 '*' + *
query.include('*');
+ * * @param {...String|Array} key The name(s) of the key(s) to include. * @return {Parse.Query} Returns the query, so you can chain this call. */ @@ -1338,6 +1342,10 @@ class ParseQuery { this._include.push(key); } }); + if (this._include.includes('*')) { + this._includeAll = true; + this._include.splice(this._include.indexOf('*'), 1); + } return this; } @@ -1347,8 +1355,7 @@ class ParseQuery { * @return {Parse.Query} Returns the query, so you can chain this call. */ includeAll(): ParseQuery { - this._includeAll = true; - return this; + return this.include('*'); } /** From 8c2b01070dfd2f428a7215f422bd2eed49b5cafd Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 10 Aug 2018 12:59:21 -0500 Subject: [PATCH 3/4] removed unnessary fields --- src/ParseQuery.js | 18 +----------------- src/__tests__/ParseQuery-test.js | 6 +++--- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/ParseQuery.js b/src/ParseQuery.js index 384fd2f72..dc3822079 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -183,7 +183,6 @@ class ParseQuery { className: string; _where: any; _include: Array; - _includeAll: boolean; _select: Array; _limit: number; _skip: number; @@ -217,7 +216,6 @@ 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 = {}; @@ -282,9 +280,6 @@ 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(','); } @@ -334,10 +329,6 @@ class ParseQuery { this._include = json.include.split(","); } - if (json.includeAll) { - this._includeAll = true; - } - if (json.keys) { this._select = json.keys.split(","); } @@ -356,7 +347,7 @@ class ParseQuery { for (let key in json) { if (json.hasOwnProperty(key)) { - if (["where", "include", "includeAll", "keys", "limit", "skip", "order"].indexOf(key) === -1) { + if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) { this._extraOptions[key] = json[key]; } } @@ -688,9 +679,6 @@ 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; @@ -1342,10 +1330,6 @@ class ParseQuery { this._include.push(key); } }); - if (this._include.includes('*')) { - this._includeAll = true; - this._include.splice(this._include.indexOf('*'), 1); - } return this; } diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js index f38123262..d2e7396ed 100644 --- a/src/__tests__/ParseQuery-test.js +++ b/src/__tests__/ParseQuery-test.js @@ -905,11 +905,11 @@ describe('ParseQuery', () => { const json = q.toJSON(); expect(json).toEqual({ where: {}, - includeAll: true, + include: '*', }); const q2 = new ParseQuery('Item'); q2.withJSON(json); - expect(q2._includeAll).toBe(true); + expect(q2._include).toEqual(['*']); }); it('can use extraOptions', () => { @@ -1326,7 +1326,7 @@ describe('ParseQuery', () => { limit: 100, order: 'objectId', keys: 'size,name', - includeAll: true, + include: '*', where: { size: { $in: ['small', 'medium'] From d5c3c01c1b387ce6c7cd34c9530972115b4bca3e Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 10 Aug 2018 15:30:44 -0500 Subject: [PATCH 4/4] include server version --- src/ParseQuery.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ParseQuery.js b/src/ParseQuery.js index dc3822079..af74aedc7 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -1316,7 +1316,8 @@ 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 '*' + * You can include all nested Parse.Objects by passing in '*'. + * Requires Parse Server 3.0.0+ *
query.include('*');
* * @param {...String|Array} key The name(s) of the key(s) to include. @@ -1336,6 +1337,8 @@ class ParseQuery { /** * 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 {