Skip to content

Commit 28f0d26

Browse files
authored
fix: relation constraints in compound queries Parse.Query.or, Parse.Query.and not working (#8203)
1 parent fb50332 commit 28f0d26

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

Diff for: spec/ParseRelation.spec.js

+43
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,49 @@ describe('Parse.Relation testing', () => {
410410
});
411411
});
412412

413+
it('or queries with base constraint on relation field', async () => {
414+
const ChildObject = Parse.Object.extend('ChildObject');
415+
const childObjects = [];
416+
for (let i = 0; i < 10; i++) {
417+
childObjects.push(new ChildObject({ x: i }));
418+
}
419+
await Parse.Object.saveAll(childObjects);
420+
const ParentObject = Parse.Object.extend('ParentObject');
421+
const parent = new ParentObject();
422+
parent.set('x', 4);
423+
const relation = parent.relation('toChilds');
424+
relation.add(childObjects[0]);
425+
relation.add(childObjects[1]);
426+
relation.add(childObjects[2]);
427+
428+
const parent2 = new ParentObject();
429+
parent2.set('x', 3);
430+
const relation2 = parent2.relation('toChilds');
431+
relation2.add(childObjects[0]);
432+
relation2.add(childObjects[1]);
433+
relation2.add(childObjects[2]);
434+
435+
const parents = [];
436+
parents.push(parent);
437+
parents.push(parent2);
438+
parents.push(new ParentObject());
439+
440+
await Parse.Object.saveAll(parents);
441+
const query1 = new Parse.Query(ParentObject);
442+
query1.equalTo('x', 4);
443+
const query2 = new Parse.Query(ParentObject);
444+
query2.equalTo('x', 3);
445+
446+
const query = Parse.Query.or(query1, query2);
447+
query.equalTo('toChilds', childObjects[2]);
448+
449+
const list = await query.find();
450+
const objectIds = list.map(item => item.id);
451+
expect(objectIds.indexOf(parent.id)).not.toBe(-1);
452+
expect(objectIds.indexOf(parent2.id)).not.toBe(-1);
453+
equal(list.length, 2, 'There should be 2 results');
454+
});
455+
413456
it('Get query on relation using un-fetched parent object', done => {
414457
// Setup data model
415458
const Wheel = Parse.Object.extend('Wheel');

Diff for: src/Controllers/DatabaseController.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -929,32 +929,32 @@ class DatabaseController {
929929
reduceInRelation(className: string, query: any, schema: any): Promise<any> {
930930
// Search for an in-relation or equal-to-relation
931931
// Make it sequential for now, not sure of paralleization side effects
932+
const promises = [];
932933
if (query['$or']) {
933934
const ors = query['$or'];
934-
return Promise.all(
935-
ors.map((aQuery, index) => {
935+
promises.push(
936+
...ors.map((aQuery, index) => {
936937
return this.reduceInRelation(className, aQuery, schema).then(aQuery => {
937938
query['$or'][index] = aQuery;
938939
});
939940
})
940-
).then(() => {
941-
return Promise.resolve(query);
942-
});
941+
);
943942
}
944943
if (query['$and']) {
945944
const ands = query['$and'];
946-
return Promise.all(
947-
ands.map((aQuery, index) => {
945+
promises.push(
946+
...ands.map((aQuery, index) => {
948947
return this.reduceInRelation(className, aQuery, schema).then(aQuery => {
949948
query['$and'][index] = aQuery;
950949
});
951950
})
952-
).then(() => {
953-
return Promise.resolve(query);
954-
});
951+
);
955952
}
956953

957-
const promises = Object.keys(query).map(key => {
954+
const otherKeys = Object.keys(query).map(key => {
955+
if (key === '$and' || key === '$or') {
956+
return;
957+
}
958958
const t = schema.getExpectedType(className, key);
959959
if (!t || t.type !== 'Relation') {
960960
return Promise.resolve(query);
@@ -1016,7 +1016,7 @@ class DatabaseController {
10161016
});
10171017
});
10181018

1019-
return Promise.all(promises).then(() => {
1019+
return Promise.all([...promises, ...otherKeys]).then(() => {
10201020
return Promise.resolve(query);
10211021
});
10221022
}

0 commit comments

Comments
 (0)