Skip to content

PG: Support for multiple projection in aggregate #4469

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

Merged
merged 2 commits into from
Dec 29, 2017
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
22 changes: 21 additions & 1 deletion spec/ParseQuery.Aggregate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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: {
Expand Down
35 changes: 17 additions & 18 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
}
Expand Down Expand Up @@ -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"');
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1500,7 +1500,6 @@ export class PostgresStorageAdapter {
columns.push(`AVG(${transformAggregateField(value.$avg)}) AS "${field}"`);
}
}
columns.join();
} else {
columns.push('*');
}
Expand Down Expand Up @@ -1546,7 +1545,7 @@ export class PostgresStorageAdapter {
}
}

const qs = `SELECT ${columns} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern} ${groupPattern}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you do not need to join the column there? Or is it the default for ES6 template strings when it comes to arrays?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I reject this? Sorry, I'm not quite sure what I'm doing when it comes to reviews :)

Copy link
Member Author

@dplewis dplewis Dec 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I did join the columns here. They weren't joined before properly.

const qs = `SELECT ${columns.join()} FROM $1:name ${wherePattern} ${sortPattern} ${limitPattern} ${skipPattern} ${groupPattern}`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitaly-t Joined here

debug(qs, values);
return this._client.map(qs, values, a => this.postgresObjectToParseObject(className, a, schema))
.then(results => {
Expand Down