Skip to content
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

Issue #7458: Nested $and not working in pouchdb-find. #7505

Merged
merged 4 commits into from Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 39 additions & 4 deletions packages/node_modules/pouchdb-selector-core/src/utils.js
Expand Up @@ -196,17 +196,52 @@ function mergeEq(value, fieldMatchers) {
fieldMatchers.$eq = value;
}

//#7458: execute function mergeAndedSelectors on nested $and
function mergeAndedSelectorsNested(obj) {
for (const prop in obj) {
if (Array.isArray(obj)) {
for (var i in obj) {
if (obj[i]['$and']) {
obj[i] = mergeAndedSelectors(obj[i]['$and']);
}
}
}
const value = obj[prop];
if (typeof value === 'object') {
mergeAndedSelectorsNested(value); // <- recursive call
}
}
return obj;
}

//#7458: determine id $and is present in selector (at any level)
function isAndInSelector(obj, isAnd) {
for (const prop in obj) {
if (prop === '$and') {
isAnd = true;
}
const value = obj[prop];
if (typeof value === 'object') {
isAnd = isAndInSelector(value, isAnd); // <- recursive call
}
}
return isAnd;
}

//
// normalize the selector
//
function massageSelector(input) {
var result = clone(input);
var wasAnded = false;
if ('$and' in result) {
result = mergeAndedSelectors(result['$and']);
wasAnded = true;
}
//#7458: if $and is present in selector (at any level) merge nested $and
if (isAndInSelector(result, false)) {
result = mergeAndedSelectorsNested(result);
if ('$and' in result) {
result = mergeAndedSelectors(result['$and'])
}
wasAnded = true;
}

['$or', '$nor'].forEach(function (orOrNor) {
if (orOrNor in result) {
Expand Down
54 changes: 54 additions & 0 deletions tests/find/test-suite-1/test.or.js
Expand Up @@ -68,5 +68,59 @@ testCases.push(function (dbType, context) {
});
});

it('#7458 should do $or with nested $and', function () {
var db = context.db;
return db.find({
selector: {
"$or": [
{ "name": {$eq: "Link"} },
{
"$and": [
{"name": "Mario"},
{"rank": 5}
]
}
]
}
}).then(function (res) {
var docs = res.docs.map(function (doc) {
return {
_id: doc._id
};
});
docs.should.deep.equal([
{'_id': 'link'},
{'_id': 'mario'},
]);
});
});

it('#7458 should do $or with nested $and, with explicit $eq', function () {
var db = context.db;
return db.find({
selector: {
"$or": [
{ "name": {$eq: "Link"} },
{
"$and": [
{"name": {$eq: "Mario"}},
{"rank": {$eq: 5}}
]
}
]
}
}).then(function (res) {
var docs = res.docs.map(function (doc) {
return {
_id: doc._id
};
});
docs.should.deep.equal([
{'_id': 'link'},
{'_id': 'mario'},
]);
});
});

});
});