Skip to content

Commit

Permalink
backbone style is BAD!
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Mar 3, 2016
1 parent 43f014a commit ded60a6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 83 deletions.
108 changes: 49 additions & 59 deletions spec/ParseRelation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,46 +254,40 @@ describe('Parse.Relation testing', () => {
childObjects.push(new ChildObject({x: i}));
}

Parse.Object.saveAll(childObjects, {
success: function() {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
relation.add(childObjects[0]);
relation.add(childObjects[1]);
relation.add(childObjects[2]);
var parent2 = new ParentObject();
parent2.set("x", 3);
var relation2 = parent2.relation("child");
relation2.add(childObjects[4]);
relation2.add(childObjects[5]);
relation2.add(childObjects[6]);

var otherChild2 = parent2.relation("otherChild");
otherChild2.add(childObjects[0]);
otherChild2.add(childObjects[1]);
otherChild2.add(childObjects[2]);
Parse.Object.saveAll(childObjects).then(() => {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
relation.add(childObjects[0]);
relation.add(childObjects[1]);
relation.add(childObjects[2]);
var parent2 = new ParentObject();
parent2.set("x", 3);
var relation2 = parent2.relation("child");
relation2.add(childObjects[4]);
relation2.add(childObjects[5]);
relation2.add(childObjects[6]);

var otherChild2 = parent2.relation("otherChild");
otherChild2.add(childObjects[0]);
otherChild2.add(childObjects[1]);
otherChild2.add(childObjects[2]);

var parents = [];
parents.push(parent);
parents.push(parent2);
Parse.Object.saveAll(parents, {
success: function() {
var query = new Parse.Query(ParentObject);
var objects = [];
objects.push(childObjects[0]);
query.containedIn("child", objects);
query.containedIn("otherChild", [childObjects[0]]);
query.find({
success: function(list) {
equal(list.length, 2, "There should be 2 results");
done();
}
});
}
});
}
var parents = [];
parents.push(parent);
parents.push(parent2);
return Parse.Object.saveAll(parents);
}).then(() => {
var query = new Parse.Query(ParentObject);
var objects = [];
objects.push(childObjects[0]);
query.containedIn("child", objects);
query.containedIn("otherChild", [childObjects[0]]);
return query.find();
}).then((list) => {
equal(list.length, 2, "There should be 2 results");
done();
});
});

Expand All @@ -304,8 +298,7 @@ describe('Parse.Relation testing', () => {
childObjects.push(new ChildObject({x: i}));
}

Parse.Object.saveAll(childObjects, {
success: function() {
Parse.Object.saveAll(childObjects).then(() => {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
Expand All @@ -323,25 +316,22 @@ describe('Parse.Relation testing', () => {
parents.push(parent2);
parents.push(new ParentObject());

Parse.Object.saveAll(parents, {
success: function() {
var query1 = new Parse.Query(ParentObject);
query1.containedIn("toChilds", [childObjects[2]]);
var query2 = new Parse.Query(ParentObject);
query2.equalTo("toChild", childObjects[2]);
var query = Parse.Query.or(query1, query2);
query.find({
success: function(list) {
list = list.filter(function(item){
return item.id == parent.id || item.id == parent2.id;
});
equal(list.length, 2, "There should be 2 results");
done();
}
});
}
return Parse.Object.saveAll(parents).then(() => {
var query1 = new Parse.Query(ParentObject);
query1.containedIn("toChilds", [childObjects[2]]);
var query2 = new Parse.Query(ParentObject);
query2.equalTo("toChild", childObjects[2]);
var query = Parse.Query.or(query1, query2);
return query.find({
success: function(list) {
list = list.filter(function(item){
return item.id == parent.id || item.id == parent2.id;
});
equal(list.length, 2, "There should be 2 results");
done();
}
});
});
}
});
});

Expand Down
48 changes: 24 additions & 24 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,31 +408,31 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
}));
}

return Object.keys(query).reduce((promise, key) => {
return promise.then(() => {
if (query[key] &&
(query[key]['$in'] || 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;
if (query[key]['$in']) {
relatedIds = query[key]['$in'].map(r => r.objectId);
} else {
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);
return Promise.resolve(query);
});
let promises = Object.keys(query).map((key) => {
if (query[key] && (query[key]['$in'] || query[key].__type == 'Pointer')) {
let t = schema.getExpectedType(className, key);
let match = t ? t.match(/^relation<(.*)>$/) : false;
if (!match) {
return Promise.resolve(query);
}
});
}, Promise.resolve()).then(() => {
let relatedClassName = match[1];
let relatedIds;
if (query[key]['$in']) {
relatedIds = query[key]['$in'].map(r => r.objectId);
} else {
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);
return Promise.resolve(query);
});
}
return Promise.resolve(query);
})

return Promise.all(promises).then(() => {
return Promise.resolve(query);
})
};
Expand Down

0 comments on commit ded60a6

Please sign in to comment.