Skip to content

Commit

Permalink
Merge pull request #4 from zishone/exists-operator
Browse files Browse the repository at this point in the history
Add Exists operator.
  • Loading branch information
Fizcko authored May 2, 2020
2 parents 69b9f2f + 03a1f03 commit 08ae29d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ It's a query language that introduces basic and logical operators. This is perfe

#### Additionals operators
- Like : =~ (to match regex values)
- Exists : =exists= (to check if property exists)

###### NOTE
Parenthesized expression can be used to define the precedence.
Expand Down Expand Up @@ -79,6 +80,10 @@ try{
rsqlMongoDB('lastName=~do*');
//=> { "lastName": { $regex: "do*" } }

// Exists operator
rsqlMongoDB('childs=exists=true');
//=> { "childs": { $exists: true } }

// Groups
rsqlMongoDB('(firstName=="john";lastName=="doe"),(firstName==janne;lastName==doe)');
//=> { $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] }
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsql-mongodb",
"version": "1.2.0",
"version": "1.3.0",
"description": "Converting RSQL queries to MongoDB queries",
"main": "rsql-mongodb.js",
"typings": "rsql-mongodb.ts",
Expand All @@ -17,6 +17,7 @@
"test-cov-ci": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
},
"author": "Fizcko",
"contributors": ["zishone (https://github.com/zishone)"],
"license": "MIT",
"repository": "https://github.com/Fizcko/rsql-mongodb",
"bugs": {
Expand Down
5 changes: 4 additions & 1 deletion rsql-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ module.exports = function (input) {
}

// Split the query
var rsqlOperators = /(.*)(==|!=|=gt=|=ge=|=lt=|=le=|=in=|=out=|=~)(.*)/g;
var rsqlOperators = /(.*)(==|!=|=gt=|=ge=|=lt=|=le=|=in=|=out=|=~|=exists=)(.*)/g;
var rsqlQuery = rsqlOperators.exec(outputTab[i]);

try {
Expand Down Expand Up @@ -250,6 +250,9 @@ module.exports = function (input) {
case "=~":
mongoOperatorQuery[exp1] = { $regex: typedExp2 };
break;
case "=exists=":
mongoOperatorQuery[exp1] = { $exists: typedExp2 };
break;
default:
throw "Operator not supported."
}
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ describe('rsql-mongodb', function () {
it("Test operator Like ('=~')", function () {
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*" } });
});
it("Test operator Exists ('=exists=')", function () {
expect(rsqlMongoDB('childs=exists=true')).to.deep.include({ "childs": { $exists: true } });
expect(rsqlMongoDB('childs=exists=false')).to.deep.include({ "childs": { $exists: false } });
expect(rsqlMongoDB('childs=exists=true')).to.be.a('object');

});
it("Test logical operator AND (';')", function () {
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });
});
Expand Down

0 comments on commit 08ae29d

Please sign in to comment.