Skip to content
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
### master
[Full Changelog](https://github.com/parse-community/parse-server/compare/2.8.4...master)

#### Improvements:
* Adds Pipeline Operator to Aggregate Router

### 2.8.4
[Full Changelog](https://github.com/parse-community/parse-server/compare/2.8.3...2.8.4)

#### Improvements
#### Improvements:
* Adds ability to forward errors to express handler (#4697)
* Adds ability to increment the push badge with an arbitrary value (#4889)
* Adds ability to preserve the file names when uploading (#4915)
Expand Down
69 changes: 69 additions & 0 deletions spec/AggregateRouter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const AggregateRouter = require('../lib/Routers/AggregateRouter').AggregateRouter;

describe('AggregateRouter', () => {
it('get pipeline from Array', () => {
const body = [{
group: { objectId: {} }
}];
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});

it('get pipeline from Object', () => {
const body = {
group: { objectId: {} }
};
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});

it('get pipeline from Pipeline Operator (Array)', () => {
const body = {
pipeline: [{
group: { objectId: {} }
}]
};
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});

it('get pipeline from Pipeline Operator (Object)', () => {
const body = {
pipeline: {
group: { objectId: {} }
}
};
const expected = [ { $group: { _id: {} } } ];
const result = AggregateRouter.getPipeline(body);
expect(result).toEqual(expected);
});

it('get pipeline fails multiple keys in Array stage ', () => {
const body = [{
group: { objectId: {} },
match: { name: 'Test' },
}];
try {
AggregateRouter.getPipeline(body);
} catch (e) {
expect(e.message).toBe('Pipeline stages should only have one key found group, match');
}
});

it('get pipeline fails multiple keys in Pipeline Operator Array stage ', () => {
const body = {
pipeline: [{
group: { objectId: {} },
match: { name: 'Test' },
}]
};
try {
AggregateRouter.getPipeline(body);
} catch (e) {
expect(e.message).toBe('Pipeline stages should only have one key found group, match');
}
});
});
18 changes: 18 additions & 0 deletions spec/ParseQuery.Aggregate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ describe('Parse.Query Aggregate testing', () => {
}).catch(done.fail);
});

it('group by pipeline operator', async () => {
const options = Object.assign({}, masterKeyOptions, {
body: {
pipeline: {
group: { objectId: '$name' },
}
}
});
const resp = await rp.get(Parse.serverURL + '/aggregate/TestObject', options);
expect(resp.results.length).toBe(3);
expect(resp.results[0].hasOwnProperty('objectId')).toBe(true);
expect(resp.results[1].hasOwnProperty('objectId')).toBe(true);
expect(resp.results[2].hasOwnProperty('objectId')).toBe(true);
expect(resp.results[0].objectId).not.toBe(undefined);
expect(resp.results[1].objectId).not.toBe(undefined);
expect(resp.results[2].objectId).not.toBe(undefined);
});

it('group by empty object', (done) => {
const obj = new TestObject();
const pipeline = [{
Expand Down
61 changes: 44 additions & 17 deletions src/Routers/AggregateRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as middleware from '../middlewares';
import Parse from 'parse/node';
import UsersRouter from './UsersRouter';

const BASE_KEYS = ['where', 'distinct'];
const BASE_KEYS = ['where', 'distinct', 'pipeline'];

const PIPELINE_KEYS = [
'addFields',
Expand Down Expand Up @@ -41,24 +41,10 @@ export class AggregateRouter extends ClassesRouter {
handleFind(req) {
const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
const options = {};
let pipeline = [];

if (Array.isArray(body)) {
pipeline = body.map((stage) => {
const stageName = Object.keys(stage)[0];
return this.transformStage(stageName, stage);
});
} else {
const stages = [];
for (const stageName in body) {
stages.push(this.transformStage(stageName, body));
}
pipeline = stages;
}
if (body.distinct) {
options.distinct = String(body.distinct);
}
options.pipeline = pipeline;
options.pipeline = AggregateRouter.getPipeline(body);
if (typeof body.where === 'string') {
body.where = JSON.parse(body.where);
}
Expand All @@ -72,7 +58,48 @@ export class AggregateRouter extends ClassesRouter {
});
}

transformStage(stageName, stage) {
/* Builds a pipeline from the body. Originally the body could be passed as a single object,
* and now we support many options
*
* Array
*
* body: [{
* group: { objectId: '$name' },
* }]
*
* Object
*
* body: {
* group: { objectId: '$name' },
* }
*
*
* Pipeline Operator with an Array or an Object
*
* body: {
* pipeline: {
* group: { objectId: '$name' },
* }
* }
*
*/
static getPipeline(body) {
let pipeline = body.pipeline || body;

if (!Array.isArray(pipeline)) {
pipeline = Object.keys(pipeline).map((key) => { return { [key]: pipeline[key] } });
}

return pipeline.map((stage) => {
const keys = Object.keys(stage);
if (keys.length != 1) {
throw new Error(`Pipeline stages should only have one key found ${keys.join(', ')}`);
}
return AggregateRouter.transformStage(keys[0], stage);
});
}

static transformStage(stageName, stage) {
if (ALLOWED_KEYS.indexOf(stageName) === -1) {
throw new Parse.Error(
Parse.Error.INVALID_QUERY,
Expand Down