-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
4e207d3
commit 7223add
Showing
8 changed files
with
675 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |