Support for nested .select() calls (#2737)

* Reproduction for #1567

* Recursive handling of nested pointer keys in select

* Better support for multi-level nested keys

* Adds support for selecting columns natively (mongo)

* Support for postgres column selections

* Filter-out empty keys for pg
This commit is contained in:
Florent Vilmart
2016-09-24 13:43:49 -04:00
committed by GitHub
parent 4974dbea37
commit 9c522be00d
7 changed files with 133 additions and 29 deletions

View File

@@ -13,8 +13,8 @@ export default class MongoCollection {
// none, then build the geoindex.
// This could be improved a lot but it's not clear if that's a good
// idea. Or even if this behavior is a good idea.
find(query, { skip, limit, sort } = {}) {
return this._rawFind(query, { skip, limit, sort })
find(query, { skip, limit, sort, keys } = {}) {
return this._rawFind(query, { skip, limit, sort, keys })
.catch(error => {
// Check for "no geoindex" error
if (error.code != 17007 && !error.message.match(/unable to find index for .geoNear/)) {
@@ -30,14 +30,18 @@ export default class MongoCollection {
index[key] = '2d';
return this._mongoCollection.createIndex(index)
// Retry, but just once.
.then(() => this._rawFind(query, { skip, limit, sort }));
.then(() => this._rawFind(query, { skip, limit, sort, keys }));
});
}
_rawFind(query, { skip, limit, sort } = {}) {
return this._mongoCollection
_rawFind(query, { skip, limit, sort, keys } = {}) {
let findOperation = this._mongoCollection
.find(query, { skip, limit, sort })
.toArray();
if (keys) {
findOperation = findOperation.project(keys);
}
return findOperation.toArray();
}
count(query, { skip, limit, sort } = {}) {

View File

@@ -320,12 +320,16 @@ export class MongoStorageAdapter {
}
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
find(className, schema, query, { skip, limit, sort }) {
find(className, schema, query, { skip, limit, sort, keys }) {
schema = convertParseSchemaToMongoSchema(schema);
let mongoWhere = transformWhere(className, query, schema);
let mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));
let mongoKeys = _.reduce(keys, (memo, key) => {
memo[transformKey(className, key, schema)] = 1;
return memo;
}, {});
return this._adaptiveCollection(className)
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort }))
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort, keys: mongoKeys }))
.then(objects => objects.map(object => mongoObjectToParseObject(className, object, schema)))
}

View File

@@ -11,9 +11,11 @@ const transformKey = (className, fieldName, schema) => {
case 'updatedAt': return '_updated_at';
case 'sessionToken': return '_session_token';
}
if (schema.fields[fieldName] && schema.fields[fieldName].__type == 'Pointer') {
fieldName = '_p_' + fieldName;
} else if (schema.fields[fieldName] && schema.fields[fieldName].type == 'Pointer') {
fieldName = '_p_' + fieldName;
}
return fieldName;