Skip to content

Commit 04f8673

Browse files
dplewisvitaly-t
authored andcommitted
PG: Support for multiple projection in aggregate (#4469)
1 parent 6ba9399 commit 04f8673

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ describe('Parse.Query Aggregate testing', () => {
254254
rp.get(Parse.serverURL + '/aggregate/TestObject', options)
255255
.then((resp) => {
256256
resp.results.forEach((result) => {
257-
expect(result.name !== undefined).toBe(true);
257+
expect(result.objectId).not.toBe(undefined);
258+
expect(result.name).not.toBe(undefined);
258259
expect(result.sender).toBe(undefined);
259260
expect(result.size).toBe(undefined);
260261
expect(result.score).toBe(undefined);
@@ -263,6 +264,25 @@ describe('Parse.Query Aggregate testing', () => {
263264
}).catch(done.fail);
264265
});
265266

267+
it('multiple project query', (done) => {
268+
const options = Object.assign({}, masterKeyOptions, {
269+
body: {
270+
project: { name: 1, score: 1, sender: 1 },
271+
}
272+
});
273+
rp.get(Parse.serverURL + '/aggregate/TestObject', options)
274+
.then((resp) => {
275+
resp.results.forEach((result) => {
276+
expect(result.objectId).not.toBe(undefined);
277+
expect(result.name).not.toBe(undefined);
278+
expect(result.score).not.toBe(undefined);
279+
expect(result.sender).not.toBe(undefined);
280+
expect(result.size).toBe(undefined);
281+
});
282+
done();
283+
}).catch(done.fail);
284+
});
285+
266286
it('project with group query', (done) => {
267287
const options = Object.assign({}, masterKeyOptions, {
268288
body: {

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -723,15 +723,15 @@ export class PostgresStorageAdapter {
723723
});
724724
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join()})`;
725725
const values = [className, ...valuesArray];
726-
726+
727727
return conn.task('create-table', function * (t) {
728728
try {
729729
yield self._ensureSchemaCollectionExists(t);
730730
yield t.none(qs, values);
731731
} catch(error) {
732-
if (error.code !== PostgresDuplicateRelationError) {
733-
throw error;
734-
}
732+
if (error.code !== PostgresDuplicateRelationError) {
733+
throw error;
734+
}
735735
// ELSE: Table already exists, must have been created by a different request. Ignore the error.
736736
}
737737
yield t.tx('create-table-tx', tx => {
@@ -755,14 +755,14 @@ export class PostgresStorageAdapter {
755755
postgresType: parseTypeToPostgresType(type)
756756
});
757757
} catch(error) {
758-
if (error.code === PostgresRelationDoesNotExistError) {
759-
return yield self.createClass(className, {fields: {[fieldName]: type}}, t);
760-
}
761-
if (error.code !== PostgresDuplicateColumnError) {
762-
throw error;
763-
}
764-
// Column already exists, created by other request. Carry on to see if it's the right type.
765-
};
758+
if (error.code === PostgresRelationDoesNotExistError) {
759+
return yield self.createClass(className, {fields: {[fieldName]: type}}, t);
760+
}
761+
if (error.code !== PostgresDuplicateColumnError) {
762+
throw error;
763+
}
764+
// Column already exists, created by other request. Carry on to see if it's the right type.
765+
}
766766
} else {
767767
yield t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
768768
}
@@ -794,7 +794,7 @@ export class PostgresStorageAdapter {
794794
const now = new Date().getTime();
795795
const helpers = this._pgp.helpers;
796796
debug('deleteAllClasses');
797-
797+
798798
return this._client.task('delete-all-classes', function * (t) {
799799
try {
800800
const results = yield t.any('SELECT * FROM "_SCHEMA"');
@@ -811,8 +811,8 @@ export class PostgresStorageAdapter {
811811
// No _SCHEMA collection. Don't delete anything.
812812
}
813813
}).then(() => {
814-
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
815-
});
814+
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
815+
});
816816
}
817817

818818
// Remove the column and all the data. For Relations, the _Join collection is handled
@@ -860,7 +860,7 @@ export class PostgresStorageAdapter {
860860
return this._client.task('get-all-classes', function * (t) {
861861
yield self._ensureSchemaCollectionExists(t);
862862
return yield t.map('SELECT * FROM "_SCHEMA"', null, row => toParseSchema({ className: row.className, ...row.schema }));
863-
});
863+
});
864864
}
865865

866866
// Return a promise for the schema with the given name, in Parse format. If
@@ -1500,7 +1500,6 @@ export class PostgresStorageAdapter {
15001500
columns.push(`AVG(${transformAggregateField(value.$avg)}) AS "${field}"`);
15011501
}
15021502
}
1503-
columns.join();
15041503
} else {
15051504
columns.push('*');
15061505
}
@@ -1546,7 +1545,7 @@ export class PostgresStorageAdapter {
15461545
}
15471546
}
15481547

1549-
const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern} ${groupPattern}`;
1548+
const qs = `SELECT ${columns.join()} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern} ${groupPattern}`;
15501549
debug(qs, values);
15511550
return this._client.map(qs, values, a => this.postgresObjectToParseObject(className, a, schema))
15521551
.then(results => {

0 commit comments

Comments
 (0)