Skip to content

Commit 6a39cfe

Browse files
committed
test and code
1 parent 1594afe commit 6a39cfe

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

spec/ParseQuery.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4318,6 +4318,40 @@ describe('Parse.Query testing', () => {
43184318
}
43194319
});
43204320

4321+
it('deeply nested Pointers (issue #7413)', async function (done) {
4322+
const parent = new Parse.Object('Parent');
4323+
const child1 = new Parse.Object('Child');
4324+
const child2 = new Parse.Object('Child');
4325+
4326+
parent.set('subDocument', { child: child1 });
4327+
parent.set('children', [
4328+
{ child: child1, count: 2 },
4329+
{ child: child2, count: 3 },
4330+
]);
4331+
await parent.save();
4332+
4333+
// equalTo
4334+
const resultsEq = await new Parse.Query('Parent').equalTo('children.child', child1).find();
4335+
expect(resultsEq.length).toBe(1);
4336+
4337+
// direct subDocument equalTo
4338+
const resultsSubDocument = await new Parse.Query('Parent')
4339+
.equalTo('subDocument.child', child1)
4340+
.find();
4341+
expect(resultsSubDocument.length).toBe(1);
4342+
4343+
// subDocument in
4344+
const resultsSubDocumentIn = await new Parse.Query('Parent')
4345+
.containedIn('subDocument.child', [child1])
4346+
.find();
4347+
expect(resultsSubDocumentIn.length).toBe(1);
4348+
4349+
// containedIn
4350+
const results = await new Parse.Query('Parent').containedIn('children.child', [child1]).find();
4351+
expect(results.length).toBe(1);
4352+
done();
4353+
});
4354+
43214355
it('include with *', async () => {
43224356
const child1 = new TestObject({ foo: 'bar', name: 'ac' });
43234357
const child2 = new TestObject({ foo: 'baz', name: 'flo' });

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ function transformQueryKeyValue(className, key, value, schema, count = false) {
316316
}
317317

318318
// Handle query constraints
319-
const transformedConstraint = transformConstraint(value, field, count);
319+
const transformedConstraint = transformConstraint(value, key, field, count);
320320
if (transformedConstraint !== CannotTransform) {
321321
if (transformedConstraint.$text) {
322322
return { key: '$text', value: transformedConstraint.$text };
@@ -766,19 +766,24 @@ function relativeTimeToDate(text, now = new Date()) {
766766
// If it is not a valid constraint but it could be a valid something
767767
// else, return CannotTransform.
768768
// inArray is whether this is an array field.
769-
function transformConstraint(constraint, field, count = false) {
770-
const inArray = field && field.type && field.type === 'Array';
769+
function transformConstraint(constraint, restKey, field, count = false) {
771770
if (typeof constraint !== 'object' || !constraint) {
772771
return CannotTransform;
773772
}
774-
const transformFunction = inArray ? transformInteriorAtom : transformTopLevelAtom;
773+
774+
// transformer
775+
const inArray = field && field.type && field.type === 'Array';
776+
const inSubDocument = restKey.includes('.');
777+
const transformFunction =
778+
inArray || inSubDocument ? transformInteriorAtom : transformTopLevelAtom;
775779
const transformer = atom => {
776780
const result = transformFunction(atom, field);
777781
if (result === CannotTransform) {
778782
throw new Parse.Error(Parse.Error.INVALID_JSON, `bad atom: ${JSON.stringify(atom)}`);
779783
}
780784
return result;
781785
};
786+
782787
// keys is the constraints in reverse alphabetical order.
783788
// This is a hack so that:
784789
// $regex is handled before $options

0 commit comments

Comments
 (0)