Skip to content

Commit fcd49ae

Browse files
committed
feat: Serialize aggregation results via EJSON when rawValues is true
1 parent 8c63f79 commit fcd49ae

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,21 @@ describe('Parse.Query Aggregate testing', () => {
541541
expect(results.length).toBe(0);
542542
});
543543

544+
it_id('f01a0001-0005-0005-0005-000000000005')(it_exclude_dbs(['postgres']))('rawValues: true serializes BSON Date in results as `{ $date: iso }`', async () => {
545+
const obj = new TestObject();
546+
await obj.save();
547+
const iso = new Date(obj.createdAt.getTime() + 1).toISOString();
548+
const pipeline = [
549+
{ $match: { objectId: obj.id, createdAt: { $lte: { $date: iso } } } },
550+
{ $project: { _id: 1, _created_at: 1 } },
551+
];
552+
const query = new Parse.Query('TestObject');
553+
const results = await query.aggregate(pipeline, { rawValues: true, useMasterKey: true });
554+
expect(results.length).toBe(1);
555+
// EJSON-serialized date marker, not Parse `{ __type: 'Date', iso }` encoding.
556+
expect(results[0]._created_at).toEqual(jasmine.objectContaining({ $date: jasmine.any(String) }));
557+
});
558+
544559
it_only_db('postgres')(
545560
'can group by any date field postgres (it does not work if you have dirty data)', // rows in your collection with non date data in the field that is supposed to be a date
546561
done => {

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,9 @@ export class MongoStorageAdapter implements StorageAdapter {
980980
})
981981
)
982982
.then(results => {
983+
if (rawValues) {
984+
return results;
985+
}
983986
results.forEach(result => {
984987
if (Object.prototype.hasOwnProperty.call(result, '_id')) {
985988
if (isPointerField && result._id) {
@@ -998,7 +1001,12 @@ export class MongoStorageAdapter implements StorageAdapter {
9981001
});
9991002
return results;
10001003
})
1001-
.then(objects => objects.map(object => mongoObjectToParseObject(className, object, schema)))
1004+
.then(objects => {
1005+
if (rawValues) {
1006+
return objects.map(obj => EJSON.serialize(obj));
1007+
}
1008+
return objects.map(object => mongoObjectToParseObject(className, object, schema));
1009+
})
10021010
.catch(err => this.handleError(err));
10031011
}
10041012

src/Routers/AggregateRouter.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ export class AggregateRouter extends ClassesRouter {
4949
req.info.clientSDK,
5050
req.info.context
5151
);
52-
for (const result of response.results) {
53-
if (typeof result === 'object') {
54-
UsersRouter.removeHiddenProperties(result);
52+
if (!options.rawValues) {
53+
for (const result of response.results) {
54+
if (typeof result === 'object') {
55+
UsersRouter.removeHiddenProperties(result);
56+
}
5557
}
5658
}
5759
return { response };

0 commit comments

Comments
 (0)