Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Query IncludeAll #4838

Merged
merged 2 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3684,6 +3684,75 @@ describe('Parse.Query testing', () => {
});
});

it("includeAll", (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

note to self: add rule on " '. Surprised the default airbnb we use doesn't have an opinion....

const child1 = new TestObject({ foo: 'bar', name: 'al' });
const child2 = new TestObject({ foo: 'baz', name: 'flo' });
const child3 = new TestObject({ foo: 'bad', name: 'mo' });
const parent = new Container({ child1, child2, child3 });
Parse.Object.saveAll([parent, child1, child2, child3]).then(() => {
const options = Object.assign({}, masterKeyOptions, {
body: {
where: { objectId: parent.id },
includeAll: true,
}
});
return rp.get(Parse.serverURL + "/classes/Container", options);
}).then((resp) => {
const result = resp.results[0];
equal(result.child1.foo, 'bar');
equal(result.child2.foo, 'baz');
equal(result.child3.foo, 'bad');
equal(result.child1.name, 'al');
equal(result.child2.name, 'flo');
equal(result.child3.name, 'mo');
done();
});
});

it('select nested keys 2 level includeAll', (done) => {
const Foobar = new Parse.Object('Foobar');
const BarBaz = new Parse.Object('Barbaz');
const Bazoo = new Parse.Object('Bazoo');
const Tang = new Parse.Object('Tang');

Bazoo.set('some', 'thing');
Bazoo.set('otherSome', 'value');
Bazoo.save().then(() => {
BarBaz.set('key', 'value');
BarBaz.set('otherKey', 'value');
BarBaz.set('bazoo', Bazoo);
return BarBaz.save();
}).then(() => {
Tang.set('clan', 'wu');
return Tang.save();
}).then(() => {
Foobar.set('foo', 'bar');
Foobar.set('fizz', 'buzz');
Foobar.set('barBaz', BarBaz);
Foobar.set('group', Tang);
return Foobar.save();
}).then((savedFoobar) => {
const options = Object.assign({}, masterKeyOptions, {
body: {
where: { objectId: savedFoobar.id },
includeAll: true,
keys: 'fizz,barBaz.key,barBaz.bazoo.some',
}
});
return rp.get(Parse.serverURL + "/classes/Foobar", options);
}).then((resp) => {
const result = resp.results[0];
equal(result.group.clan, 'wu');
equal(result.foo, undefined);
equal(result.fizz, 'buzz');
equal(result.barBaz.key, 'value');
equal(result.barBaz.otherKey, undefined);
equal(result.barBaz.bazoo.some, 'thing');
equal(result.barBaz.bazoo.otherSome, undefined);
done();
})
});

it('select nested keys 2 level without include (issue #3185)', function(done) {
const Foobar = new Parse.Object('Foobar');
const BarBaz = new Parse.Object('Barbaz');
Expand Down
29 changes: 29 additions & 0 deletions src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
}

this.doCount = false;
this.includeAll = false;

// The format for this.include is not the same as the format for the
// include option - it's the paths we should include, in order,
Expand Down Expand Up @@ -86,6 +87,9 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
case 'count':
this.doCount = true;
break;
case 'includeAll':
this.includeAll = true;
break;
case 'distinct':
case 'pipeline':
case 'skip':
Expand Down Expand Up @@ -149,6 +153,8 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
RestQuery.prototype.execute = function(executeOptions) {
return Promise.resolve().then(() => {
return this.buildRestWhere();
}).then(() => {
return this.handleIncludeAll();
}).then(() => {
return this.runFind(executeOptions);
}).then(() => {
Expand Down Expand Up @@ -552,6 +558,29 @@ RestQuery.prototype.runCount = function() {
});
};

RestQuery.prototype.handleIncludeAll = function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

function block comment would be nice....

if (!this.includeAll) {
return;
}
return this.config.database.loadSchema()
.then(schemaController => schemaController.getOneSchema(this.className))
.then(schema => {
const includeFields = [];
const keyFields = [];
for (const field in schema.fields) {
if (schema.fields[field].type && schema.fields[field].type === 'Pointer') {
includeFields.push([field]);
keyFields.push(field);
}
}
// Add fields to include, keys, remove dups
this.include = [...new Set([...this.include, ...includeFields])];
if (this.keys) {
Copy link
Contributor

Choose a reason for hiding this comment

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

comment like:

// if this.keys not set, then all keys already included

assuming I've got that right.

this.keys = [...new Set([...this.keys, ...keyFields])];
}
});
};

// Augments this.response with data at the paths provided in this.include.
RestQuery.prototype.handleInclude = function() {
if (this.include.length == 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/Routers/ClassesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class ClassesRouter extends PromiseRouter {

static optionsFromBody(body) {
const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
'include', 'redirectClassNameForKey', 'where'];
'include', 'includeAll', 'redirectClassNameForKey', 'where'];

for (const key of Object.keys(body)) {
if (allowConstraints.indexOf(key) === -1) {
Expand Down Expand Up @@ -128,6 +128,9 @@ export class ClassesRouter extends PromiseRouter {
if (body.include) {
options.include = String(body.include);
}
if (body.includeAll) {
options.includeAll = true;
}
return options;
}

Expand Down