From 565fa473f848e1b67610833ab80787b4c79c5015 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Thu, 13 Feb 2025 15:47:39 -0600 Subject: [PATCH 1/3] fix: `Parse.Query.findAll` not returning all objects with `json: true` option --- integration/test/ParseQueryTest.js | 10 ++++++--- src/ParseQuery.ts | 35 +++++++----------------------- src/__tests__/ParseQuery-test.js | 11 +++++++++- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/integration/test/ParseQueryTest.js b/integration/test/ParseQueryTest.js index 27434f920..f2c3c4204 100644 --- a/integration/test/ParseQueryTest.js +++ b/integration/test/ParseQueryTest.js @@ -1493,13 +1493,17 @@ describe('Parse Query', () => { it('can return all objects with findAll', async () => { const objs = [...Array(101)].map(() => new Parse.Object('Container')); - await Parse.Object.saveAll(objs); - const query = new Parse.Query('Container'); - const result = await query.findAll(); + assert.equal(result.length, 101); + }); + it('can return all objects with findAll json option', async () => { + const objs = [...Array(101)].map(() => new Parse.Object('Container')); + await Parse.Object.saveAll(objs); + const query = new Parse.Query('Container'); + const result = await query.findAll({ json: true, batchSize: 100 }); assert.equal(result.length, 101); }); diff --git a/src/ParseQuery.ts b/src/ParseQuery.ts index 0d5c9d855..3f5424289 100644 --- a/src/ParseQuery.ts +++ b/src/ParseQuery.ts @@ -721,6 +721,7 @@ class ParseQuery { * be used for this request. *
  • sessionToken: A valid session token, used for making a request on * behalf of a specific user. + *
  • json: Return raw json without converting to Parse.Object * * @returns {Promise} A promise that is resolved with the results when * the query completes. @@ -924,33 +925,9 @@ class ParseQuery { return Promise.reject(error); } - const query = new ParseQuery(this.className); - query._limit = options.batchSize || 100; - query._include = [...this._include]; - query._exclude = [...this._exclude]; - if (this._select) { - query._select = [...this._select]; - } - query._hint = this._hint; - query._where = {}; - for (const attr in this._where) { - const val = this._where[attr]; - if (Array.isArray(val)) { - query._where[attr] = val.map(v => { - return v; - }); - } else if (val && typeof val === 'object') { - const conditionMap = {}; - query._where[attr] = conditionMap; - for (const cond in val) { - conditionMap[cond] = val[cond]; - } - } else { - query._where[attr] = val; - } - } - + const query = ParseQuery.fromJSON(this.className, this.toJSON()); query.ascending('objectId'); + query._limit = options.batchSize || 100; const findOptions = ParseObject._getRequestOptions(options); let finished = false; @@ -965,7 +942,11 @@ class ParseQuery { Promise.resolve(previousResults.length > 0 && callback(previousResults)), ]); if (results.length >= query._limit) { - query.greaterThan('objectId', results[results.length - 1].id); + if (findOptions.json) { + query.greaterThan('objectId', (results[results.length - 1] as any).objectId); + } else { + query.greaterThan('objectId', results[results.length - 1].id); + } previousResults = results; } else if (results.length > 0) { await Promise.resolve(callback(results)); diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js index 31071e4ee..1239fed46 100644 --- a/src/__tests__/ParseQuery-test.js +++ b/src/__tests__/ParseQuery-test.js @@ -1805,7 +1805,11 @@ describe('ParseQuery', () => { q.hint('_id_'); q.exclude('foo'); - await q.findAll(); + const [result1, result2] = await q.findAll(); + expect(result1.id).toBeDefined(); + expect(result2.id).toBeDefined(); + expect(result1.objectId).toBeUndefined(); + expect(result2.objectId).toBeUndefined(); expect(findMock).toHaveBeenCalledTimes(1); const [className, params, options] = findMock.mock.calls[0]; expect(className).toBe('Item'); @@ -1835,6 +1839,9 @@ describe('ParseQuery', () => { }, }); expect(options.requestTask).toBeDefined(); + const [json] = await q.findAll({ json: true }); + expect(json.objectId).toBeDefined(); + expect(json.id).toBeUndefined(); }); it('passes options through to the REST API', async () => { @@ -1842,6 +1849,7 @@ describe('ParseQuery', () => { useMasterKey: true, sessionToken: '1234', batchSize: 50, + json: true, }; const q = new ParseQuery('Item'); await q.findAll(batchOptions); @@ -1855,6 +1863,7 @@ describe('ParseQuery', () => { }); expect(options.useMasterKey).toBe(true); expect(options.sessionToken).toEqual('1234'); + expect(options.json).toEqual(true); }); it('only makes one request when the results fit in one page', async () => { From 4414cc7d84a1051809350771acaba6cc8da7ac71 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Thu, 13 Feb 2025 17:19:29 -0600 Subject: [PATCH 2/3] improve coverage --- src/__tests__/ParseQuery-test.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js index 1239fed46..be0c6646b 100644 --- a/src/__tests__/ParseQuery-test.js +++ b/src/__tests__/ParseQuery-test.js @@ -1805,11 +1805,7 @@ describe('ParseQuery', () => { q.hint('_id_'); q.exclude('foo'); - const [result1, result2] = await q.findAll(); - expect(result1.id).toBeDefined(); - expect(result2.id).toBeDefined(); - expect(result1.objectId).toBeUndefined(); - expect(result2.objectId).toBeUndefined(); + await q.findAll(); expect(findMock).toHaveBeenCalledTimes(1); const [className, params, options] = findMock.mock.calls[0]; expect(className).toBe('Item'); @@ -1839,9 +1835,6 @@ describe('ParseQuery', () => { }, }); expect(options.requestTask).toBeDefined(); - const [json] = await q.findAll({ json: true }); - expect(json.objectId).toBeDefined(); - expect(json.id).toBeUndefined(); }); it('passes options through to the REST API', async () => { @@ -1883,6 +1876,17 @@ describe('ParseQuery', () => { const results = await q.findAll(); expect(results.map(obj => obj.attributes.size)).toEqual(['medium', 'small']); }); + + it('Returns all objects with json', async () => { + const q = new ParseQuery('Item'); + const results = await q.findAll({ json: true, batchSize: 2 }); + expect(results.length).toEqual(3); + expect(findMock).toHaveBeenCalledTimes(2); + results.map(result => { + expect(result.id).toBeUndefined(); + expect(result.objectId).toBeDefined(); + }); + }); }); it('can iterate over results with each()', done => { From 569f47796482ab9f72999ad8b562faa4f7c7b035 Mon Sep 17 00:00:00 2001 From: Manuel <5673677+mtrezza@users.noreply.github.com> Date: Fri, 14 Feb 2025 15:06:28 +0100 Subject: [PATCH 3/3] docs Signed-off-by: Manuel <5673677+mtrezza@users.noreply.github.com> --- src/ParseQuery.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ParseQuery.ts b/src/ParseQuery.ts index 3f5424289..648060b9b 100644 --- a/src/ParseQuery.ts +++ b/src/ParseQuery.ts @@ -721,7 +721,7 @@ class ParseQuery { * be used for this request. *
  • sessionToken: A valid session token, used for making a request on * behalf of a specific user. - *
  • json: Return raw json without converting to Parse.Object + *
  • json: Return raw JSON without converting to Parse.Object. * * @returns {Promise} A promise that is resolved with the results when * the query completes.