Support for Aggregate Queries (#4207)
* Support for Aggregate Queries * improve pg and coverage * Mongo 3.4 aggregates and tests * replace _id with objectId * improve tests for objectId * project with group query * typo
This commit is contained in:
committed by
Florent Vilmart
parent
4e207d32a7
commit
7223add446
77
src/Routers/AggregateRouter.js
Normal file
77
src/Routers/AggregateRouter.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import ClassesRouter from './ClassesRouter';
|
||||
import rest from '../rest';
|
||||
import * as middleware from '../middlewares';
|
||||
import Parse from 'parse/node';
|
||||
|
||||
const ALLOWED_KEYS = [
|
||||
'where',
|
||||
'distinct',
|
||||
'project',
|
||||
'match',
|
||||
'redact',
|
||||
'limit',
|
||||
'skip',
|
||||
'unwind',
|
||||
'group',
|
||||
'sample',
|
||||
'sort',
|
||||
'geoNear',
|
||||
'lookup',
|
||||
'out',
|
||||
'indexStats',
|
||||
'facet',
|
||||
'bucket',
|
||||
'bucketAuto',
|
||||
'sortByCount',
|
||||
'addFields',
|
||||
'replaceRoot',
|
||||
'count',
|
||||
'graphLookup',
|
||||
];
|
||||
|
||||
export class AggregateRouter extends ClassesRouter {
|
||||
|
||||
handleFind(req) {
|
||||
const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
|
||||
const options = {};
|
||||
const pipeline = [];
|
||||
|
||||
for (const key in body) {
|
||||
if (ALLOWED_KEYS.indexOf(key) === -1) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid parameter for query: ${key}`);
|
||||
}
|
||||
if (key === 'group') {
|
||||
if (body[key].hasOwnProperty('_id')) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_QUERY,
|
||||
`Invalid parameter for query: group. Please use objectId instead of _id`
|
||||
);
|
||||
}
|
||||
if (!body[key].hasOwnProperty('objectId')) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_QUERY,
|
||||
`Invalid parameter for query: group. objectId is required`
|
||||
);
|
||||
}
|
||||
body[key]._id = body[key].objectId;
|
||||
delete body[key].objectId;
|
||||
}
|
||||
pipeline.push({ [`$${key}`]: body[key] });
|
||||
}
|
||||
if (body.distinct) {
|
||||
options.distinct = String(body.distinct);
|
||||
}
|
||||
options.pipeline = pipeline;
|
||||
if (typeof body.where === 'string') {
|
||||
body.where = JSON.parse(body.where);
|
||||
}
|
||||
return rest.find(req.config, req.auth, this.className(req), body.where, options, req.info.clientSDK)
|
||||
.then((response) => { return { response }; });
|
||||
}
|
||||
|
||||
mountRoutes() {
|
||||
this.route('GET','/aggregate/:className', middleware.promiseEnforceMasterKeyAccess, req => { return this.handleFind(req); });
|
||||
}
|
||||
}
|
||||
|
||||
export default AggregateRouter;
|
||||
Reference in New Issue
Block a user