Skip to content

Commit

Permalink
🎉 fixes #1349
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Apr 4, 2016
1 parent ca6269f commit aa154c7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
10 changes: 9 additions & 1 deletion spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Parse.Query testing', () => {
});
});

it("notEqualTo with Relation is working", function(done) {
fit("notEqualTo with Relation is working", function(done) {
var user = new Parse.User();
user.setPassword("asdf");
user.setUsername("zxcv");
Expand Down Expand Up @@ -101,6 +101,14 @@ describe('Parse.Query testing', () => {
// This test fails on 2.2.4
equal(results.length, 0);
});
}).then(function(){
var query = new Parse.Query(Cake);
// User2 is a hater of everything so we should receive 0
query.notContainedIn("liker", [user1]);
return query.find().then(function(results){
// This test fails on 2.2.4
equal(results.length, 1);
});
}).then(function(){
done();
})
Expand Down
38 changes: 36 additions & 2 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,22 +444,33 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
}

let promises = Object.keys(query).map((key) => {
if (query[key] && (query[key]['$in'] || query[key].__type == 'Pointer')) {
if (query[key] && (query[key]['$in'] || query[key]['$ne'] || query[key]['$nin'] || query[key].__type == 'Pointer')) {
let t = schema.getExpectedType(className, key);
let match = t ? t.match(/^relation<(.*)>$/) : false;
if (!match) {
return Promise.resolve(query);
}
let relatedClassName = match[1];
let relatedIds;
let isNegation = false;
if (query[key]['$in']) {
relatedIds = query[key]['$in'].map(r => r.objectId);
} else if (query[key]['$nin']) {
isNegation = true;
relatedIds = query[key]['$nin'].map(r => r.objectId);
} else if (query[key]['$ne']) {
isNegation = true;
relatedIds = [query[key]['$ne'].objectId];
} else {
relatedIds = [query[key].objectId];
}
return this.owningIds(className, key, relatedIds).then((ids) => {
delete query[key];
this.addInObjectIdsIds(ids, query);
if (isNegation) {
this.addNotInObjectIdsIds(ids, query);
} else {
this.addInObjectIdsIds(ids, query);
}
return Promise.resolve(query);
});
}
Expand Down Expand Up @@ -520,6 +531,29 @@ DatabaseController.prototype.addInObjectIdsIds = function(ids = null, query) {
return query;
}

DatabaseController.prototype.addNotInObjectIdsIds = function(ids = null, query) {
let idsFromNin = query.objectId && query.objectId['$nin'] ? query.objectId['$nin'] : null;
let allIds = [idsFromNin, ids].filter(list => list !== null);
let totalLength = allIds.reduce((memo, list) => memo + list.length, 0);

let idsIntersection = [];
if (totalLength > 125) {
idsIntersection = intersect.big(allIds);
} else {
idsIntersection = intersect(allIds);
}

// Need to make sure we don't clobber existing $lt or other constraints on objectId.
// Clobbering $eq, $in and shorthand $eq (query.objectId === 'string') constraints
// is expected though.
if (!('objectId' in query) || typeof query.objectId === 'string') {
query.objectId = {};
}
query.objectId['$nin'] = idsIntersection;

return query;
}

// Runs a query on the database.
// Returns a promise that resolves to a list of items.
// Options:
Expand Down

0 comments on commit aa154c7

Please sign in to comment.