Fixes bug when querying equalTo on objectId and relation
- Adds $eq operator in transform - Makes $eq operator on objectId when adding $in operator
This commit is contained in:
@@ -329,6 +329,46 @@ describe('Parse.Relation testing', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("query on pointer and relation fields with equal bis", (done) => {
|
||||||
|
var ChildObject = Parse.Object.extend("ChildObject");
|
||||||
|
var childObjects = [];
|
||||||
|
for (var i = 0; i < 10; i++) {
|
||||||
|
childObjects.push(new ChildObject({x: i}));
|
||||||
|
}
|
||||||
|
|
||||||
|
Parse.Object.saveAll(childObjects).then(() => {
|
||||||
|
var ParentObject = Parse.Object.extend("ParentObject");
|
||||||
|
var parent = new ParentObject();
|
||||||
|
parent.set("x", 4);
|
||||||
|
var relation = parent.relation("toChilds");
|
||||||
|
relation.add(childObjects[0]);
|
||||||
|
relation.add(childObjects[1]);
|
||||||
|
relation.add(childObjects[2]);
|
||||||
|
|
||||||
|
var parent2 = new ParentObject();
|
||||||
|
parent2.set("x", 3);
|
||||||
|
parent2.relation("toChilds").add(childObjects[2]);
|
||||||
|
|
||||||
|
var parents = [];
|
||||||
|
parents.push(parent);
|
||||||
|
parents.push(parent2);
|
||||||
|
parents.push(new ParentObject());
|
||||||
|
|
||||||
|
return Parse.Object.saveAll(parents).then(() => {
|
||||||
|
var query = new Parse.Query(ParentObject);
|
||||||
|
query.equalTo("objectId", parent2.id);
|
||||||
|
// childObjects[2] is in 2 relations
|
||||||
|
// before the fix, that woul yield 2 results
|
||||||
|
query.equalTo("toChilds", childObjects[2]);
|
||||||
|
|
||||||
|
return query.find().then((list) => {
|
||||||
|
equal(list.length, 1, "There should be 1 result");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("or queries on pointer and relation fields", (done) => {
|
it("or queries on pointer and relation fields", (done) => {
|
||||||
var ChildObject = Parse.Object.extend("ChildObject");
|
var ChildObject = Parse.Object.extend("ChildObject");
|
||||||
var childObjects = [];
|
var childObjects = [];
|
||||||
|
|||||||
@@ -455,13 +455,18 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
|
|||||||
|
|
||||||
DatabaseController.prototype.addInObjectIdsIds = function(ids, query) {
|
DatabaseController.prototype.addInObjectIdsIds = function(ids, query) {
|
||||||
if (typeof query.objectId == 'string') {
|
if (typeof query.objectId == 'string') {
|
||||||
query.objectId = {'$in': [query.objectId]};
|
// Add equality op as we are sure
|
||||||
|
// we had a constraint on that one
|
||||||
|
query.objectId = {'$eq': query.objectId};
|
||||||
}
|
}
|
||||||
query.objectId = query.objectId || {};
|
query.objectId = query.objectId || {};
|
||||||
let queryIn = [].concat(query.objectId['$in'] || [], ids || []);
|
let queryIn = [].concat(query.objectId['$in'] || [], ids || []);
|
||||||
// make a set and spread to remove duplicates
|
// make a set and spread to remove duplicates
|
||||||
query.objectId = {'$in': [...new Set(queryIn)]};
|
// replace the $in operator as other constraints
|
||||||
return query;
|
// may be set
|
||||||
|
query.objectId['$in'] = [...new Set(queryIn)];
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runs a query on the database.
|
// Runs a query on the database.
|
||||||
|
|||||||
@@ -412,6 +412,7 @@ function transformConstraint(constraint, inArray) {
|
|||||||
case '$gte':
|
case '$gte':
|
||||||
case '$exists':
|
case '$exists':
|
||||||
case '$ne':
|
case '$ne':
|
||||||
|
case '$eq':
|
||||||
answer[key] = transformAtom(constraint[key], true,
|
answer[key] = transformAtom(constraint[key], true,
|
||||||
{inArray: inArray});
|
{inArray: inArray});
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user