Sanitize objectId in

- if objectId is set in query, move it to $in array
- refactors to addInObjectIdsIds
This commit is contained in:
Florent Vilmart
2016-03-03 08:40:30 -05:00
parent c6406b9e1a
commit 907a05c57a
3 changed files with 109 additions and 7 deletions

View File

@@ -417,9 +417,8 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
relatedIds = [query[key].objectId];
}
return this.owningIds(className, key, relatedIds).then((ids) => {
delete query[key];
query.objectId = Object.assign({'$in': []}, query.objectId);
query.objectId['$in'] = query.objectId['$in'].concat(ids);
delete query[key];
this.addInObjectIdsIds(ids, query);
return Promise.resolve(query);
});
}
@@ -448,15 +447,23 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
relatedTo.key,
relatedTo.object.objectId).then((ids) => {
delete query['$relatedTo'];
query.objectId = query.objectId || {};
let queryIn = query.objectId['$in'] || [];
queryIn = queryIn.concat(ids);
query['objectId'] = {'$in': queryIn};
this.addInObjectIdsIds(ids, query);
return this.reduceRelationKeys(className, query);
});
}
};
DatabaseController.prototype.addInObjectIdsIds = function(ids, query) {
if (typeof query.objectId == 'string') {
query.objectId = {'$in': [query.objectId]};
}
query.objectId = query.objectId || {};
let queryIn = [].concat(query.objectId['$in'] || [], ids || []);
// make a set and spread to remove duplicates
query.objectId = {'$in': [...new Set(queryIn)]};
return query;
}
// Runs a query on the database.
// Returns a promise that resolves to a list of items.
// Options: