Skip to content

Commit 8ec07b8

Browse files
authored
Support pointer in aggregate query (#4493)
1 parent cb8f038 commit 8ec07b8

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ describe('Parse.Query Aggregate testing', () => {
9696
}).catch(done.fail);
9797
});
9898

99+
it('group by pointer', (done) => {
100+
const pointer1 = new TestObject();
101+
const pointer2 = new TestObject();
102+
const obj1 = new TestObject({ pointer: pointer1 });
103+
const obj2 = new TestObject({ pointer: pointer2 });
104+
const obj3 = new TestObject({ pointer: pointer1 });
105+
const pipeline = [
106+
{ group: { objectId: '$pointer' } }
107+
];
108+
Parse.Object.saveAll([pointer1, pointer2, obj1, obj2, obj3]).then(() => {
109+
const query = new Parse.Query(TestObject);
110+
return query.aggregate(pipeline);
111+
}).then((results) => {
112+
expect(results.length).toEqual(3);
113+
expect(results.some(result => result.objectId === pointer1.id)).toEqual(true);
114+
expect(results.some(result => result.objectId === pointer2.id)).toEqual(true);
115+
expect(results.some(result => result.objectId === null)).toEqual(true);
116+
done();
117+
});
118+
});
119+
99120
it('group sum query', (done) => {
100121
const options = Object.assign({}, masterKeyOptions, {
101122
body: {

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,26 @@ export class MongoStorageAdapter implements StorageAdapter {
515515
}
516516

517517
aggregate(className: string, schema: any, pipeline: any, readPreference: ?string) {
518+
let isPointerField = false;
519+
pipeline = pipeline.map((stage) => {
520+
if (stage.$group && stage.$group._id) {
521+
const field = stage.$group._id.substring(1);
522+
if (schema.fields[field] && schema.fields[field].type === 'Pointer') {
523+
isPointerField = true;
524+
stage.$group._id = `$_p_${field}`;
525+
}
526+
}
527+
return stage;
528+
});
518529
readPreference = this._parseReadPreference(readPreference);
519530
return this._adaptiveCollection(className)
520531
.then(collection => collection.aggregate(pipeline, { readPreference, maxTimeMS: this._maxTimeMS }))
521532
.then(results => {
522533
results.forEach(result => {
523534
if (result.hasOwnProperty('_id')) {
535+
if (isPointerField && result._id) {
536+
result._id = result._id.split('$')[1];
537+
}
524538
result.objectId = result._id;
525539
delete result._id;
526540
}

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,13 @@ export class PostgresStorageAdapter implements StorageAdapter {
754754
debug('schemaUpgrade', { className, schema });
755755
conn = conn || this._client;
756756
const self = this;
757-
757+
758758
return conn.tx('schema-upgrade', function * (t) {
759759
const columns = yield t.map('SELECT column_name FROM information_schema.columns WHERE table_name = $<className>', { className }, a => a.column_name);
760760
const newColumns = Object.keys(schema.fields)
761761
.filter(item => columns.indexOf(item) === -1)
762762
.map(fieldName => self.addFieldIfNotExists(className, fieldName, schema.fields[fieldName], t));
763-
763+
764764
yield t.batch(newColumns);
765765
});
766766
}
@@ -896,7 +896,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
896896
return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className })
897897
.then(result => {
898898
if (result.length !== 1) {
899-
throw undefined;
899+
throw undefined;
900900
}
901901
return result[0].schema;
902902
})
@@ -1267,12 +1267,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
12671267
const createValue = Object.assign({}, query, update);
12681268
return this.createObject(className, schema, createValue)
12691269
.catch(error => {
1270-
// ignore duplicate value errors as it's upsert
1271-
if (error.code !== Parse.Error.DUPLICATE_VALUE) {
1272-
throw error;
1273-
}
1274-
return this.findOneAndUpdate(className, schema, query, update);
1275-
});
1270+
// ignore duplicate value errors as it's upsert
1271+
if (error.code !== Parse.Error.DUPLICATE_VALUE) {
1272+
throw error;
1273+
}
1274+
return this.findOneAndUpdate(className, schema, query, update);
1275+
});
12761276
}
12771277

12781278
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys }: QueryOptions) {
@@ -1452,7 +1452,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
14521452
throw error;
14531453
}
14541454
return 0;
1455-
});
1455+
});
14561456
}
14571457

14581458
distinct(className: string, schema: SchemaType, query: QueryType, fieldName: string) {

0 commit comments

Comments
 (0)