Support Distinct for special fields (#5144)

* Support Distinct for special fields

* update changelog
This commit is contained in:
Diamond Lewis
2018-10-26 10:21:42 -05:00
committed by GitHub
parent 961abda4eb
commit daab3781a2
3 changed files with 38 additions and 7 deletions

View File

@@ -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)

View File

@@ -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' },

View File

@@ -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);
});