Skip to content

Commit

Permalink
Support Distinct for special fields (#5144)
Browse files Browse the repository at this point in the history
* Support Distinct for special fields

* update changelog
  • Loading branch information
dplewis authored Oct 26, 2018
1 parent 961abda commit daab378
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#### Improvements:
* Fixes issue that would prevent users with large number of roles to resolve all of them [@Moumouls]() (#5131, #5132)

* Fixes distinct query on special fields ([#5144](https://github.com/parse-community/parse-server/pull/5144))

### 3.1.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/3.0.0...3.1.0)
Expand Down
30 changes: 30 additions & 0 deletions spec/ParseQuery.Aggregate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,36 @@ describe('Parse.Query Aggregate testing', () => {
.catch(done.fail);
});

it('distinct objectId', async () => {
const query = new Parse.Query(TestObject);
const results = await query.distinct('objectId');
expect(results.length).toBe(4);
});

it('distinct createdAt', async () => {
const object1 = new TestObject({ createdAt_test: true });
await object1.save();
const object2 = new TestObject({ createdAt_test: true });
await object2.save();
const query = new Parse.Query(TestObject);
query.equalTo('createdAt_test', true);
const results = await query.distinct('createdAt');
expect(results.length).toBe(2);
});

it('distinct updatedAt', async () => {
const object1 = new TestObject({ updatedAt_test: true });
await object1.save();
const object2 = new TestObject();
await object2.save();
object2.set('updatedAt_test', true);
await object2.save();
const query = new Parse.Query(TestObject);
query.equalTo('updatedAt_test', true);
const results = await query.distinct('updatedAt');
expect(results.length).toBe(2);
});

it('distinct null field', done => {
const options = Object.assign({}, masterKeyOptions, {
body: { distinct: 'distinctField' },
Expand Down
13 changes: 7 additions & 6 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,19 +710,20 @@ export class MongoStorageAdapter implements StorageAdapter {
schema = convertParseSchemaToMongoSchema(schema);
const isPointerField =
schema.fields[fieldName] && schema.fields[fieldName].type === 'Pointer';
if (isPointerField) {
fieldName = `_p_${fieldName}`;
}
const transformField = transformKey(className, fieldName, schema);

return this._adaptiveCollection(className)
.then(collection =>
collection.distinct(fieldName, transformWhere(className, query, schema))
collection.distinct(
transformField,
transformWhere(className, query, schema)
)
)
.then(objects => {
objects = objects.filter(obj => obj != null);
return objects.map(object => {
if (isPointerField) {
const field = fieldName.substring(3);
return transformPointerString(schema, field, object);
return transformPointerString(schema, fieldName, object);
}
return mongoObjectToParseObject(className, object, schema);
});
Expand Down

0 comments on commit daab378

Please sign in to comment.