From a27e4d054d6c93f704049e65de8ebfcc92dcf368 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Thu, 28 Dec 2017 22:56:28 -0600 Subject: [PATCH] PG: Support for multiple projection in aggregate --- spec/ParseQuery.Aggregate.spec.js | 22 +++++++++++- .../Postgres/PostgresStorageAdapter.js | 35 +++++++++---------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/spec/ParseQuery.Aggregate.spec.js b/spec/ParseQuery.Aggregate.spec.js index 6d8a53e5d5..c47f53ab5d 100644 --- a/spec/ParseQuery.Aggregate.spec.js +++ b/spec/ParseQuery.Aggregate.spec.js @@ -254,7 +254,8 @@ describe('Parse.Query Aggregate testing', () => { rp.get(Parse.serverURL + '/aggregate/TestObject', options) .then((resp) => { resp.results.forEach((result) => { - expect(result.name !== undefined).toBe(true); + expect(result.objectId).not.toBe(undefined); + expect(result.name).not.toBe(undefined); expect(result.sender).toBe(undefined); expect(result.size).toBe(undefined); expect(result.score).toBe(undefined); @@ -263,6 +264,25 @@ describe('Parse.Query Aggregate testing', () => { }).catch(done.fail); }); + it('multiple project query', (done) => { + const options = Object.assign({}, masterKeyOptions, { + body: { + project: { name: 1, score: 1, sender: 1 }, + } + }); + rp.get(Parse.serverURL + '/aggregate/TestObject', options) + .then((resp) => { + resp.results.forEach((result) => { + expect(result.objectId).not.toBe(undefined); + expect(result.name).not.toBe(undefined); + expect(result.score).not.toBe(undefined); + expect(result.sender).not.toBe(undefined); + expect(result.size).toBe(undefined); + }); + done(); + }).catch(done.fail); + }); + it('project with group query', (done) => { const options = Object.assign({}, masterKeyOptions, { body: { diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index f7ab358194..00542e7f6a 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -723,15 +723,15 @@ export class PostgresStorageAdapter { }); const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join()})`; const values = [className, ...valuesArray]; - + return conn.task('create-table', function * (t) { try { yield self._ensureSchemaCollectionExists(t); yield t.none(qs, values); } catch(error) { - if (error.code !== PostgresDuplicateRelationError) { - throw error; - } + if (error.code !== PostgresDuplicateRelationError) { + throw error; + } // ELSE: Table already exists, must have been created by a different request. Ignore the error. } yield t.tx('create-table-tx', tx => { @@ -755,14 +755,14 @@ export class PostgresStorageAdapter { postgresType: parseTypeToPostgresType(type) }); } catch(error) { - if (error.code === PostgresRelationDoesNotExistError) { - return yield self.createClass(className, {fields: {[fieldName]: type}}, t); - } - if (error.code !== PostgresDuplicateColumnError) { - throw error; - } - // Column already exists, created by other request. Carry on to see if it's the right type. - }; + if (error.code === PostgresRelationDoesNotExistError) { + return yield self.createClass(className, {fields: {[fieldName]: type}}, t); + } + if (error.code !== PostgresDuplicateColumnError) { + throw error; + } + // Column already exists, created by other request. Carry on to see if it's the right type. + } } else { yield t.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); } @@ -794,7 +794,7 @@ export class PostgresStorageAdapter { const now = new Date().getTime(); const helpers = this._pgp.helpers; debug('deleteAllClasses'); - + return this._client.task('delete-all-classes', function * (t) { try { const results = yield t.any('SELECT * FROM "_SCHEMA"'); @@ -811,8 +811,8 @@ export class PostgresStorageAdapter { // No _SCHEMA collection. Don't delete anything. } }).then(() => { - debug(`deleteAllClasses done in ${new Date().getTime() - now}`); - }); + debug(`deleteAllClasses done in ${new Date().getTime() - now}`); + }); } // Remove the column and all the data. For Relations, the _Join collection is handled @@ -860,7 +860,7 @@ export class PostgresStorageAdapter { return this._client.task('get-all-classes', function * (t) { yield self._ensureSchemaCollectionExists(t); return yield t.map('SELECT * FROM "_SCHEMA"', null, row => toParseSchema({ className: row.className, ...row.schema })); - }); + }); } // Return a promise for the schema with the given name, in Parse format. If @@ -1500,7 +1500,6 @@ export class PostgresStorageAdapter { columns.push(`AVG(${transformAggregateField(value.$avg)}) AS "${field}"`); } } - columns.join(); } else { columns.push('*'); } @@ -1546,7 +1545,7 @@ export class PostgresStorageAdapter { } } - const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern} ${groupPattern}`; + const qs = `SELECT ${columns.join()} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern} ${groupPattern}`; debug(qs, values); return this._client.map(qs, values, a => this.postgresObjectToParseObject(className, a, schema)) .then(results => {