Skip to content

fix: relation constraints in compound queries Parse.Query.or, Parse.Query.and not working #8203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions spec/ParseRelation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,49 @@ describe('Parse.Relation testing', () => {
});
});

it('or queries with base constraint on relation field', async () => {
const ChildObject = Parse.Object.extend('ChildObject');
const childObjects = [];
for (let i = 0; i < 10; i++) {
childObjects.push(new ChildObject({ x: i }));
}
await Parse.Object.saveAll(childObjects);
const ParentObject = Parse.Object.extend('ParentObject');
const parent = new ParentObject();
parent.set('x', 4);
const relation = parent.relation('toChilds');
relation.add(childObjects[0]);
relation.add(childObjects[1]);
relation.add(childObjects[2]);

const parent2 = new ParentObject();
parent2.set('x', 3);
const relation2 = parent2.relation('toChilds');
relation2.add(childObjects[0]);
relation2.add(childObjects[1]);
relation2.add(childObjects[2]);

const parents = [];
parents.push(parent);
parents.push(parent2);
parents.push(new ParentObject());

await Parse.Object.saveAll(parents);
const query1 = new Parse.Query(ParentObject);
query1.equalTo('x', 4);
const query2 = new Parse.Query(ParentObject);
query2.equalTo('x', 3);

const query = Parse.Query.or(query1, query2);
query.equalTo('toChilds', childObjects[2]);

const list = await query.find();
const objectIds = list.map(item => item.id);
expect(objectIds.indexOf(parent.id)).not.toBe(-1);
expect(objectIds.indexOf(parent2.id)).not.toBe(-1);
equal(list.length, 2, 'There should be 2 results');
});

it('Get query on relation using un-fetched parent object', done => {
// Setup data model
const Wheel = Parse.Object.extend('Wheel');
Expand Down
24 changes: 12 additions & 12 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,32 +929,32 @@ class DatabaseController {
reduceInRelation(className: string, query: any, schema: any): Promise<any> {
// Search for an in-relation or equal-to-relation
// Make it sequential for now, not sure of paralleization side effects
const promises = [];
if (query['$or']) {
const ors = query['$or'];
return Promise.all(
ors.map((aQuery, index) => {
promises.push(
...ors.map((aQuery, index) => {
return this.reduceInRelation(className, aQuery, schema).then(aQuery => {
query['$or'][index] = aQuery;
});
})
).then(() => {
return Promise.resolve(query);
});
);
}
if (query['$and']) {
const ands = query['$and'];
return Promise.all(
ands.map((aQuery, index) => {
promises.push(
...ands.map((aQuery, index) => {
return this.reduceInRelation(className, aQuery, schema).then(aQuery => {
query['$and'][index] = aQuery;
});
})
).then(() => {
return Promise.resolve(query);
});
);
}

const promises = Object.keys(query).map(key => {
const otherKeys = Object.keys(query).map(key => {
if (key === '$and' || key === '$or') {
return;
}
const t = schema.getExpectedType(className, key);
if (!t || t.type !== 'Relation') {
return Promise.resolve(query);
Expand Down Expand Up @@ -1016,7 +1016,7 @@ class DatabaseController {
});
});

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