Skip to content

Commit c346e15

Browse files
committed
Merge pull request #1280 from jeremyjackson89/master
Single object queries to use include and keys
2 parents 02af61d + 4fd67a5 commit c346e15

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

spec/ParseQuery.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -2393,4 +2393,44 @@ describe('Parse.Query testing', () => {
23932393
done();
23942394
});
23952395
});
2396+
2397+
it('include for specific object', function(done){
2398+
var child = new Parse.Object('Child');
2399+
var parent = new Parse.Object('Parent');
2400+
child.set('foo', 'bar');
2401+
parent.set('child', child);
2402+
Parse.Object.saveAll([child, parent], function(response){
2403+
var savedParent = response[1];
2404+
var parentQuery = new Parse.Query('Parent');
2405+
parentQuery.include('child');
2406+
parentQuery.get(savedParent.id, {
2407+
success: function(parentObj) {
2408+
var childPointer = parentObj.get('child');
2409+
ok(childPointer);
2410+
equal(childPointer.get('foo'), 'bar');
2411+
done();
2412+
}
2413+
});
2414+
});
2415+
});
2416+
2417+
it('select keys for specific object', function(done){
2418+
var Foobar = new Parse.Object('Foobar');
2419+
Foobar.set('foo', 'bar');
2420+
Foobar.set('fizz', 'buzz');
2421+
Foobar.save({
2422+
success: function(savedFoobar){
2423+
var foobarQuery = new Parse.Query('Foobar');
2424+
foobarQuery.select('fizz');
2425+
foobarQuery.get(savedFoobar.id,{
2426+
success: function(foobarObj){
2427+
equal(foobarObj.get('fizz'), 'buzz');
2428+
equal(foobarObj.get('foo'), undefined);
2429+
done();
2430+
}
2431+
});
2432+
}
2433+
})
2434+
});
2435+
23962436
});

src/Routers/ClassesRouter.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import rest from '../rest';
44

55
import url from 'url';
66

7+
const ALLOWED_GET_QUERY_KEYS = ['keys', 'include'];
8+
79
export class ClassesRouter extends PromiseRouter {
810

911
handleFind(req) {
@@ -59,7 +61,23 @@ export class ClassesRouter extends PromiseRouter {
5961

6062
// Returns a promise for a {response} object.
6163
handleGet(req) {
62-
return rest.find(req.config, req.auth, req.params.className, {objectId: req.params.objectId})
64+
let body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
65+
let options = {};
66+
67+
for (let key of Object.keys(body)) {
68+
if (ALLOWED_GET_QUERY_KEYS.indexOf(key) === -1) {
69+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Improper encode of parameter');
70+
}
71+
}
72+
73+
if (typeof body.keys == 'string') {
74+
options.keys = body.keys;
75+
}
76+
if (body.include) {
77+
options.include = String(body.include);
78+
}
79+
80+
return rest.find(req.config, req.auth, req.params.className, {objectId: req.params.objectId}, options)
6381
.then((response) => {
6482
if (!response.results || response.results.length == 0) {
6583
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');

0 commit comments

Comments
 (0)