Skip to content

feat(ParseQuery): support using aggregate on top of constructed query #1170

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions integration/test/ParseQueryAggregateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ describe('Parse Aggregate Query', () => {
expect(results[0].tempPointer.value).toEqual(2);
});

it('aggregate pipeline on top of a simple query', async done => {
const pipeline = {
group: { objectId: '$name' },
};
let results = await new Parse.Query(TestObject)
.equalTo('name', 'foo')
.aggregate(pipeline);

expect(results.length).toBe(1);

results = await new Parse.Query(TestObject)
.equalTo('score', 20)
.aggregate(pipeline);

expect(results.length).toBe(1);

done();
});

it('distinct query', () => {
const query = new Parse.Query(TestObject);
return query.distinct('score').then((results) => {
Expand Down
5 changes: 5 additions & 0 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,11 @@ class ParseQuery {
throw new Error('Invalid pipeline must be Array or Object');
}

if (Object.keys(this._where || {}).length) {
if(!Array.isArray(pipeline)) pipeline = [pipeline];
pipeline.unshift({ match: this._where });
}

const params = {
pipeline,
hint: this._hint,
Expand Down