Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pointer in aggregate query #4493

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/ParseQuery.Aggregate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ describe('Parse.Query Aggregate testing', () => {
}).catch(done.fail);
});

it('group by pointer', (done) => {
const pointer1 = new TestObject();
const pointer2 = new TestObject();
const obj1 = new TestObject({ pointer: pointer1 });
const obj2 = new TestObject({ pointer: pointer2 });
const obj3 = new TestObject({ pointer: pointer1 });
const pipeline = [
{ group: { objectId: '$pointer' } }
];
Parse.Object.saveAll([pointer1, pointer2, obj1, obj2, obj3]).then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
}).then((results) => {
expect(results.length).toEqual(3);
expect(results.some(result => result.objectId === pointer1.id)).toEqual(true);
expect(results.some(result => result.objectId === pointer2.id)).toEqual(true);
expect(results.some(result => result.objectId === null)).toEqual(true);
done();
});
});

it('group sum query', (done) => {
const options = Object.assign({}, masterKeyOptions, {
body: {
Expand Down
14 changes: 14 additions & 0 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,26 @@ export class MongoStorageAdapter implements StorageAdapter {
}

aggregate(className: string, schema: any, pipeline: any, readPreference: ?string) {
let isPointerField = false;
pipeline = pipeline.map((stage) => {
if (stage.$group && stage.$group._id) {
const field = stage.$group._id.substring(1);
if (schema.fields[field] && schema.fields[field].type === 'Pointer') {
isPointerField = true;
stage.$group._id = `$_p_${field}`;
}
}
return stage;
});
readPreference = this._parseReadPreference(readPreference);
return this._adaptiveCollection(className)
.then(collection => collection.aggregate(pipeline, { readPreference, maxTimeMS: this._maxTimeMS }))
.then(results => {
results.forEach(result => {
if (result.hasOwnProperty('_id')) {
if (isPointerField && result._id) {
result._id = result._id.split('$')[1];
}
result.objectId = result._id;
delete result._id;
}
Expand Down
20 changes: 10 additions & 10 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,13 @@ export class PostgresStorageAdapter implements StorageAdapter {
debug('schemaUpgrade', { className, schema });
conn = conn || this._client;
const self = this;

return conn.tx('schema-upgrade', function * (t) {
const columns = yield t.map('SELECT column_name FROM information_schema.columns WHERE table_name = $<className>', { className }, a => a.column_name);
const newColumns = Object.keys(schema.fields)
.filter(item => columns.indexOf(item) === -1)
.map(fieldName => self.addFieldIfNotExists(className, fieldName, schema.fields[fieldName], t));

yield t.batch(newColumns);
});
}
Expand Down Expand Up @@ -896,7 +896,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
return this._client.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className })
.then(result => {
if (result.length !== 1) {
throw undefined;
throw undefined;
}
return result[0].schema;
})
Expand Down Expand Up @@ -1267,12 +1267,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
const createValue = Object.assign({}, query, update);
return this.createObject(className, schema, createValue)
.catch(error => {
// ignore duplicate value errors as it's upsert
if (error.code !== Parse.Error.DUPLICATE_VALUE) {
throw error;
}
return this.findOneAndUpdate(className, schema, query, update);
});
// ignore duplicate value errors as it's upsert
if (error.code !== Parse.Error.DUPLICATE_VALUE) {
throw error;
}
return this.findOneAndUpdate(className, schema, query, update);
});
}

find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys }: QueryOptions) {
Expand Down Expand Up @@ -1452,7 +1452,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
throw error;
}
return 0;
});
});
}

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