Skip to content

Commit

Permalink
Fix issue #6 with AND/OR operator and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizcko committed Nov 19, 2020
1 parent 8072fea commit 10c8123
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsql-mongodb",
"version": "1.4.0",
"version": "1.5.0",
"description": "Converting RSQL queries to MongoDB queries",
"main": "rsql-mongodb.js",
"typings": "rsql-mongodb.ts",
Expand Down
35 changes: 30 additions & 5 deletions rsql-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ module.exports = function (input) {

lastLogical = logicalsTab[logicalsTab.length - 1];
}

// Push the character into 'logicalsTab'
logicalsTab.push(character);

}

}
Expand All @@ -93,7 +94,13 @@ module.exports = function (input) {
}
// Else push the character into the 'logicalsTab'
else{

// Push all operator presents in 'logicalsTab' into 'outputTab'
while(logicalsTab.length > 0) {
outputTab.push(logicalsTab.pop());
}
logicalsTab.push(character);
outputTab.push(character);
}

}
Expand All @@ -120,6 +127,7 @@ module.exports = function (input) {

// Remove the open parenthesis from 'logicalsTab'
logicalsTab.pop();
outputTab.push(character);
}
}
// If the character is not an operator push it into the 'outputString' buffer
Expand Down Expand Up @@ -148,7 +156,9 @@ module.exports = function (input) {
// Define variables
var mongoStack = [];
var mongoQuery = [];

var tmpPrecedence = [];
var lastLogical = "";
var lastLogicalBeforePrecedence = ""

for(var i = 0; i < outputTab.length; i++) {

Expand All @@ -160,7 +170,7 @@ module.exports = function (input) {
switch(outputTab[i]){
case ";":
case ",":
if(i == (outputTab.length -1) || (mongoStack.length == 1 && mongoQuery.length == 1)){
if(i == (outputTab.length -1) || (mongoQuery.length == 1)){
while(mongoQuery.length > 0) {
tmpArray.push(mongoQuery.shift())
}
Expand All @@ -169,10 +179,12 @@ module.exports = function (input) {
tmpArray.push(mongoStack.shift())
}
if(outputTab[i] == ";"){
newValue['$and'] = tmpArray;
lastLogical = '$and';
newValue[lastLogical] = tmpArray;
}
else{
newValue['$or'] = tmpArray;
lastLogical = '$or';
newValue[lastLogical] = tmpArray;
}
break;
default:
Expand All @@ -182,6 +194,19 @@ module.exports = function (input) {
mongoQuery.push(newValue);

}
else if( outputTab[i] == '('){
tmpPrecedence = mongoQuery.shift();
lastLogicalBeforePrecedence = lastLogical;
}
else if( outputTab[i] == ')'){
if(tmpPrecedence){
tmpPrecedence[lastLogicalBeforePrecedence].push(mongoQuery.shift());
mongoQuery.push(tmpPrecedence);

}else{

}
}
else{

// Verify if the is no injections
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ describe('rsql-mongodb', function () {
it("Test other cases", function () {
expect(rsqlMongoDB('firstName==john,firstName==janne,firstName==jim')).to.deep.include({$or:[{"firstName":"john"},{"firstName":"janne"},{"firstName":"jim"}]});
expect(rsqlMongoDB('firstName==john,firstName==janne,firstName==jim;lastName==doe')).to.deep.include({$and:[{$or:[{"firstName":"john"},{"firstName":"janne"},{"firstName":"jim"}]},{"lastName":"doe"}]});
expect(rsqlMongoDB('a==1;(b==2,c==3)')).to.deep.include({"$and":[{"a":1},{"$or":[{"b":2},{"c":3}]}]});
expect(rsqlMongoDB('(b==2,c==3);a==1')).to.deep.include({"$and":[{"$or":[{"b":2},{"c":3}]},{"a":1}]});
});
it("Test errors", function () {
expect(function () { rsqlMongoDB('azerty') }).to.throw('Wrong RSQL query. No operator found.');
Expand Down

0 comments on commit 10c8123

Please sign in to comment.