Skip to content

Commit 08ae29d

Browse files
authored
Merge pull request #4 from zishone/exists-operator
Add Exists operator.
2 parents 69b9f2f + 03a1f03 commit 08ae29d

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

Readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ It's a query language that introduces basic and logical operators. This is perfe
3333

3434
#### Additionals operators
3535
- Like : =~ (to match regex values)
36+
- Exists : =exists= (to check if property exists)
3637

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

83+
// Exists operator
84+
rsqlMongoDB('childs=exists=true');
85+
//=> { "childs": { $exists: true } }
86+
8287
// Groups
8388
rsqlMongoDB('(firstName=="john";lastName=="doe"),(firstName==janne;lastName==doe)');
8489
//=> { $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] }

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsql-mongodb",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Converting RSQL queries to MongoDB queries",
55
"main": "rsql-mongodb.js",
66
"typings": "rsql-mongodb.ts",
@@ -17,6 +17,7 @@
1717
"test-cov-ci": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
1818
},
1919
"author": "Fizcko",
20+
"contributors": ["zishone (https://github.com/zishone)"],
2021
"license": "MIT",
2122
"repository": "https://github.com/Fizcko/rsql-mongodb",
2223
"bugs": {

rsql-mongodb.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ module.exports = function (input) {
193193
}
194194

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

199199
try {
@@ -250,6 +250,9 @@ module.exports = function (input) {
250250
case "=~":
251251
mongoOperatorQuery[exp1] = { $regex: typedExp2 };
252252
break;
253+
case "=exists=":
254+
mongoOperatorQuery[exp1] = { $exists: typedExp2 };
255+
break;
253256
default:
254257
throw "Operator not supported."
255258
}

test.js

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ describe('rsql-mongodb', function () {
5555
it("Test operator Like ('=~')", function () {
5656
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*" } });
5757
});
58+
it("Test operator Exists ('=exists=')", function () {
59+
expect(rsqlMongoDB('childs=exists=true')).to.deep.include({ "childs": { $exists: true } });
60+
expect(rsqlMongoDB('childs=exists=false')).to.deep.include({ "childs": { $exists: false } });
61+
expect(rsqlMongoDB('childs=exists=true')).to.be.a('object');
62+
63+
});
5864
it("Test logical operator AND (';')", function () {
5965
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });
6066
});

0 commit comments

Comments
 (0)