Skip to content

Commit 4caede2

Browse files
authored
feat: Add option to return raw json from queries (#1294)
* feat: Add option to return raw json from queries * integration test
1 parent 635ba56 commit 4caede2

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

integration/test/ParseQueryTest.js

+24
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ describe('Parse Query', () => {
4848
.catch(done.fail);
4949
});
5050

51+
it('can return raw json from queries', async () => {
52+
const object = new TestObject({ foo: 'bar' });
53+
await object.save();
54+
55+
const query = new Parse.Query(TestObject);
56+
const results = await query.find({ json: true });
57+
assert.strictEqual(results[0] instanceof Parse.Object, false);
58+
assert.strictEqual(results[0].foo, 'bar');
59+
assert.strictEqual(results[0].className, 'TestObject');
60+
assert.strictEqual(results[0].objectId, object.id);
61+
62+
let result = await query.first({ json: true });
63+
assert.strictEqual(result instanceof Parse.Object, false);
64+
assert.strictEqual(result.foo, 'bar');
65+
assert.strictEqual(result.className, 'TestObject');
66+
assert.strictEqual(result.objectId, object.id);
67+
68+
result = await query.get(object.id, { json: true });
69+
assert.strictEqual(result instanceof Parse.Object, false);
70+
assert.strictEqual(result.foo, 'bar');
71+
assert.strictEqual(result.className, 'TestObject');
72+
assert.strictEqual(result.objectId, object.id);
73+
});
74+
5175
it('can do query with count', async () => {
5276
const items = [];
5377
for (let i = 0; i < 4; i++) {

src/ParseQuery.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ class ParseQuery {
601601
* <li>sessionToken: A valid session token, used for making a request on
602602
* behalf of a specific user.
603603
* <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
604+
* <li>json: Return raw json without converting to Parse.Object
604605
* </ul>
605606
*
606607
* @returns {Promise} A promise that is resolved with the result when
@@ -619,6 +620,9 @@ class ParseQuery {
619620
if (options && options.hasOwnProperty('context') && typeof options.context === 'object') {
620621
firstOptions.context = options.context;
621622
}
623+
if (options && options.hasOwnProperty('json')) {
624+
firstOptions.json = options.json;
625+
}
622626

623627
return this.first(firstOptions).then(response => {
624628
if (response) {
@@ -640,6 +644,7 @@ class ParseQuery {
640644
* <li>sessionToken: A valid session token, used for making a request on
641645
* behalf of a specific user.
642646
* <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
647+
* <li>json: Return raw json without converting to Parse.Object
643648
* </ul>
644649
*
645650
* @returns {Promise} A promise that is resolved with the results when
@@ -686,8 +691,11 @@ class ParseQuery {
686691
if (select) {
687692
handleSelectResult(data, select);
688693
}
689-
690-
return ParseObject.fromJSON(data, !select);
694+
if (options.json) {
695+
return data;
696+
} else {
697+
return ParseObject.fromJSON(data, !select);
698+
}
691699
});
692700

693701
const count = response.count;
@@ -849,6 +857,7 @@ class ParseQuery {
849857
* <li>sessionToken: A valid session token, used for making a request on
850858
* behalf of a specific user.
851859
* <li>context: A dictionary that is accessible in Cloud Code `beforeFind` trigger.
860+
* <li>json: Return raw json without converting to Parse.Object
852861
* </ul>
853862
*
854863
* @returns {Promise} A promise that is resolved with the object when
@@ -900,8 +909,11 @@ class ParseQuery {
900909
if (select) {
901910
handleSelectResult(objects[0], select);
902911
}
903-
904-
return ParseObject.fromJSON(objects[0], !select);
912+
if (options.json) {
913+
return objects[0];
914+
} else {
915+
return ParseObject.fromJSON(objects[0], !select);
916+
}
905917
});
906918
}
907919

src/__tests__/ParseQuery-test.js

+37
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,43 @@ describe('ParseQuery', () => {
13411341
});
13421342
});
13431343

1344+
it('can return raw json from query', async () => {
1345+
CoreManager.setQueryController({
1346+
aggregate() {},
1347+
find() {
1348+
return Promise.resolve({
1349+
results: [
1350+
{
1351+
objectId: 'I1',
1352+
size: 'small',
1353+
name: 'Product 3',
1354+
},
1355+
],
1356+
});
1357+
},
1358+
});
1359+
1360+
const q = new ParseQuery('Item');
1361+
q.equalTo('size', 'small');
1362+
const results = await q.find({ json: true });
1363+
expect(results[0].objectId).toBe('I1');
1364+
expect(results[0].size).toBe('small');
1365+
expect(results[0].name).toEqual('Product 3');
1366+
expect(results[0].className).toEqual('Item');
1367+
1368+
let result = await q.first({ json: true });
1369+
expect(result.objectId).toBe('I1');
1370+
expect(result.size).toBe('small');
1371+
expect(result.name).toEqual('Product 3');
1372+
expect(result.className).toEqual('Item');
1373+
1374+
result = await q.get(result.objectId, { json: true });
1375+
expect(result.objectId).toBe('I1');
1376+
expect(result.size).toBe('small');
1377+
expect(result.name).toEqual('Product 3');
1378+
expect(result.className).toEqual('Item');
1379+
});
1380+
13441381
it('will error when getting a nonexistent object', done => {
13451382
CoreManager.setQueryController({
13461383
aggregate() {},

0 commit comments

Comments
 (0)