Skip to content

Commit

Permalink
fix: Parse.Query.distinct fails due to invalid aggregate stage 'hin…
Browse files Browse the repository at this point in the history
…t' (#9295)
  • Loading branch information
Chilldev authored Oct 22, 2024
1 parent 5e1546e commit 5f66c6a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
31 changes: 31 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

const Parse = require('parse/node');
const request = require('../lib/request');
const ParseServerRESTController = require('../lib/ParseServerRESTController').ParseServerRESTController;
const ParseServer = require('../lib/ParseServer').default;

const masterKeyHeaders = {
'X-Parse-Application-Id': 'test',
Expand Down Expand Up @@ -5275,4 +5277,33 @@ describe('Parse.Query testing', () => {
// Validate
expect(result.executionStats).not.toBeUndefined();
});

it('should query with distinct within eachBatch and direct access enabled', async () => {
await reconfigureServer({
directAccess: true,
});

Parse.CoreManager.setRESTController(
ParseServerRESTController(Parse.applicationId, ParseServer.promiseRouter({ appId: Parse.applicationId }))
);

const user = new Parse.User();
user.set('username', 'foo');
user.set('password', 'bar');
await user.save();

const score = new Parse.Object('Score');
score.set('player', user);
score.set('score', 1);
await score.save();

await new Parse.Query('_User')
.equalTo('objectId', user.id)
.eachBatch(async ([user]) => {
const score = await new Parse.Query('Score')
.equalTo('player', user)
.distinct('score', { useMasterKey: true });
expect(score).toEqual([1]);
}, { useMasterKey: true });
});
});
18 changes: 10 additions & 8 deletions src/Routers/AggregateRouter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ClassesRouter from './ClassesRouter';
import rest from '../rest';
import * as middleware from '../middlewares';
import Parse from 'parse/node';
import * as middleware from '../middlewares';
import rest from '../rest';
import ClassesRouter from './ClassesRouter';
import UsersRouter from './UsersRouter';

export class AggregateRouter extends ClassesRouter {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class AggregateRouter extends ClassesRouter {
}

/* Builds a pipeline from the body. Originally the body could be passed as a single object,
* and now we support many options
* and now we support many options.
*
* Array
*
Expand All @@ -71,17 +71,19 @@ export class AggregateRouter extends ClassesRouter {
*
* body: {
* pipeline: {
* group: { objectId: '$name' },
* $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] };
});
pipeline = Object.keys(pipeline)
.filter(key => pipeline[key] !== undefined)
.map(key => {
return { [key]: pipeline[key] };
});
}

return pipeline.map(stage => {
Expand Down

0 comments on commit 5f66c6a

Please sign in to comment.