Skip to content

Commit c58e4ae

Browse files
committed
Return object not a string
1 parent 7c918f7 commit c58e4ae

File tree

3 files changed

+96
-60
lines changed

3 files changed

+96
-60
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsql-mongodb",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Converting RSQL queries to MongoDB queries",
55
"main": "rsql-mongodb.js",
66
"typings": "rsql-mongodb.ts",

rsql-mongodb.js

+63-31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
function setType(input) {
2+
3+
var typedInput = input;
4+
5+
var matchQuotes = /^(["']{1})(.*)(["']{1})$/g;
6+
var matchQuotesResults = matchQuotes.exec(input);
7+
var matchDate = /(\d{4})-(\d{2})-(\d{2})/g;
8+
9+
if(matchQuotesResults){
10+
typedInput = matchQuotesResults[2];
11+
}
12+
else if(input === 'true'){
13+
typedInput = true;
14+
}
15+
else if(input === 'false'){
16+
typedInput = false;
17+
}
18+
else if (!isNaN(Number(input))) {
19+
typedInput = Number(input);
20+
}
21+
else if(matchDate.exec(input)){
22+
if(Date.parse(input)){
23+
typedInput = new Date('"' + input + '"');
24+
}
25+
}
26+
27+
return typedInput;
28+
}
29+
130
module.exports = function (input) {
231

332
// Define variables
@@ -92,24 +121,24 @@ module.exports = function (input) {
92121

93122
if(logicals.indexOf(outputTab[i]) !== -1){
94123

124+
var newValue = {};
125+
95126
switch(outputTab[i]){
96127
case ";":
97-
var newValue = '{ $and: [ ' + mongoStack[mongoStack.length - 2] + ' , ' + mongoStack[mongoStack.length - 1] + ' ] }';
98-
mongoStack.pop();
99-
mongoStack.pop();
100-
mongoStack.push(newValue);
128+
newValue['$and'] = [mongoStack[mongoStack.length - 2],mongoStack[mongoStack.length - 1]];
101129
break;
102130
case ",":
103-
var newValue = '{ $or: [ ' + mongoStack[mongoStack.length - 2] + ' , ' + mongoStack[mongoStack.length - 1] + ' ] }';
104-
mongoStack.pop();
105-
mongoStack.pop();
106-
mongoStack.push(newValue);
131+
newValue['$or'] = [mongoStack[mongoStack.length - 2],mongoStack[mongoStack.length - 1]];
107132
break;
108133
default:
109134
throw "Logical operator not supported."
110135
break;
111136
}
112137

138+
mongoStack.pop();
139+
mongoStack.pop();
140+
mongoStack.push(newValue);
141+
113142
}
114143
else{
115144

@@ -132,53 +161,56 @@ module.exports = function (input) {
132161

133162
}
134163
catch(e){
135-
throw "Wrong RSQL query."
164+
throw "Wrong RSQL query. No operator found."
136165
}
137166

138-
// Check if there is a date in exp2
139-
var checkString = /^(\"|\').*(\"|\')$/g;
140-
if(!checkString.exec(exp2)){
141-
var checkDate = /(\d{4})-(\d{2})-(\d{2})/g;
142-
if(checkDate.exec(exp2)){
143-
if(Date.parse(exp2)){
144-
exp2 = 'new Date("' + exp2 + '")'
145-
}
146-
}
147-
}
167+
168+
var typedExp2 = setType(exp2);
169+
var mongoQuery = {};
148170

149171
switch(operator){
150172
case "==":
151-
mongoStack.push('{ "' + exp1 + '" : ' + exp2 + ' }');
173+
mongoQuery[exp1] = typedExp2;
152174
break;
153175
case "!=":
154-
mongoStack.push('{ "' + exp1 + '": { $ne: ' + exp2 + ' } }');
176+
mongoQuery[exp1] = { $ne: typedExp2 };
155177
break;
156178
case "=gt=":
157-
mongoStack.push('{ "' + exp1 + '": { $gt: ' + exp2 + ' } }');
179+
mongoQuery[exp1] = { $gt: typedExp2 };
158180
break;
159181
case "=ge=":
160-
mongoStack.push('{ "' + exp1 + '": { $gte: ' + exp2 + ' } }');
182+
mongoQuery[exp1] = { $gte: typedExp2 };
161183
break;
162184
case "=lt=":
163-
mongoStack.push('{ "' + exp1 + '": { $lt: ' + exp2 + ' } }');
185+
mongoQuery[exp1] = { $lt: typedExp2 };
164186
break;
165187
case "=le=":
166-
mongoStack.push('{ "' + exp1 + '": { $lte: ' + exp2 + ' } }');
188+
mongoQuery[exp1] = { $lte: typedExp2 };
167189
break;
168190
case "=in=":
169-
exp2 = exp2.replace("(","[");
170-
exp2 = exp2.replace(")","]");
171-
mongoStack.push('{ "' + exp1 + '": { $in: ' + exp2 + ' } }');
191+
typedExp2 = typedExp2.replace("(","");
192+
typedExp2 = typedExp2.replace(")","");
193+
var typedValues = new Array();
194+
for ( var token of typedExp2.split(",") ) {
195+
typedValues.push(setType(token));
196+
}
197+
mongoQuery[exp1] = { $in: typedValues };
172198
break;
173199
case "=out=":
174-
exp2 = exp2.replace("(","[");
175-
exp2 = exp2.replace(")","]");
176-
mongoStack.push('{ "' + exp1 + '": { $nin: ' + exp2 + ' } }');
200+
typedExp2 = typedExp2.replace("(","");
201+
typedExp2 = typedExp2.replace(")","");
202+
var typedValues = new Array();
203+
for ( var token of typedExp2.split(",") ) {
204+
typedValues.push(setType(token));
205+
}
206+
mongoQuery[exp1] = { $nin: typedValues };
177207
break;
178208
default:
179209
throw "Operator not supported."
180210
break;
181211
}
212+
213+
mongoStack.push(mongoQuery);
182214

183215
}
184216

test.js

+32-28
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,60 @@ const rsqlMongoDB = require('./');
55

66
describe('rsql-mongodb', function () {
77
it("Test operator Equal To ('==')", function () {
8-
assert.equal(rsqlMongoDB('lastName=="doe"'), '{ "lastName" : "doe" }');
9-
assert.equal(rsqlMongoDB('birthday=="1959-10-21"'), '{ "birthday" : "1959-10-21" }');
10-
assert.equal(rsqlMongoDB('birthday==1959-10-21'), '{ "birthday" : new Date("1959-10-21") }');
11-
assert.equal(rsqlMongoDB('married==true'), '{ "married" : true }');
12-
assert.equal(rsqlMongoDB('childs==2'), '{ "childs" : 2 }');
8+
expect(rsqlMongoDB('lastName=="doe"')).to.deep.include({ "lastName" : "doe" });
9+
expect(rsqlMongoDB('birthday=="1959-10-21"')).to.deep.include({ "birthday" : "1959-10-21" });
10+
expect(rsqlMongoDB('birthday==1959-10-21')).to.be.a('object');
11+
expect(rsqlMongoDB('married==true')).to.deep.include({ "married" : true });
12+
expect(rsqlMongoDB('childs==2')).to.deep.include({ "childs" : 2 });
1313
});
1414
it("Test operator Not Equal To ('!=')", function () {
15-
assert.equal(rsqlMongoDB('lastName!="doe"'), '{ "lastName": { $ne: "doe" } }');
16-
assert.equal(rsqlMongoDB('birthday!="1959-10-21"'), '{ "birthday": { $ne: "1959-10-21" } }');
17-
assert.equal(rsqlMongoDB('birthday!=1959-10-21'), '{ "birthday": { $ne: new Date("1959-10-21") } }');
18-
assert.equal(rsqlMongoDB('married!=true'), '{ "married": { $ne: true } }');
19-
assert.equal(rsqlMongoDB('childs!=2'), '{ "childs": { $ne: 2 } }');
15+
expect(rsqlMongoDB('lastName!="doe"')).to.deep.include({ "lastName": { $ne: "doe" } });
16+
expect(rsqlMongoDB('birthday!="1959-10-21"')).to.deep.include({ "birthday": { $ne: "1959-10-21" } });
17+
expect(rsqlMongoDB('birthday!=1959-10-21')).to.be.a('object');
18+
expect(rsqlMongoDB('married!=true')).to.deep.include({ "married": { $ne: true } });
19+
expect(rsqlMongoDB('childs!=2')).to.deep.include({ "childs": { $ne: 2 } });
20+
2021
});
2122
it("Test operator Greater Than ('=gt=')", function () {
22-
assert.equal(rsqlMongoDB('birthday=gt="1959-10-21"'), '{ "birthday": { $gt: "1959-10-21" } }');
23-
assert.equal(rsqlMongoDB('birthday=gt=1959-10-21'), '{ "birthday": { $gt: new Date("1959-10-21") } }');
24-
assert.equal(rsqlMongoDB('childs=gt=2'), '{ "childs": { $gt: 2 } }');
23+
expect(rsqlMongoDB('birthday=gt="1959-10-21"')).to.deep.include({ "birthday": { $gt: "1959-10-21" } });
24+
expect(rsqlMongoDB('birthday=gt=1959-10-21')).to.be.a('object');
25+
expect(rsqlMongoDB('childs=gt=2')).to.deep.include({ "childs": { $gt: 2 } });
2526
});
2627
it("Test operator Greater Or Equal To ('=ge=')", function () {
27-
assert.equal(rsqlMongoDB('birthday=ge="1959-10-21"'), '{ "birthday": { $gte: "1959-10-21" } }');
28-
assert.equal(rsqlMongoDB('birthday=ge=1959-10-21'), '{ "birthday": { $gte: new Date("1959-10-21") } }');
29-
assert.equal(rsqlMongoDB('childs=ge=2'), '{ "childs": { $gte: 2 } }');
28+
expect(rsqlMongoDB('birthday=ge="1959-10-21"')).to.deep.include({ "birthday": { $gte: "1959-10-21" } });
29+
expect(rsqlMongoDB('birthday=ge=1959-10-21')).to.be.a('object');
30+
expect(rsqlMongoDB('childs=ge=2')).to.deep.include({ "childs": { $gte: 2 } });
3031
});
3132
it("Test operator Less Than ('=lt=')", function () {
32-
assert.equal(rsqlMongoDB('birthday=lt="1959-10-21"'), '{ "birthday": { $lt: "1959-10-21" } }');
33-
assert.equal(rsqlMongoDB('birthday=lt=1959-10-21'), '{ "birthday": { $lt: new Date("1959-10-21") } }');
34-
assert.equal(rsqlMongoDB('childs=lt=2'), '{ "childs": { $lt: 2 } }');
33+
expect(rsqlMongoDB('birthday=lt="1959-10-21"')).to.deep.include({ "birthday": { $lt: "1959-10-21" } });
34+
expect(rsqlMongoDB('birthday=lt=1959-10-21')).to.be.a('object');
35+
expect(rsqlMongoDB('childs=lt=2')).to.deep.include({ "childs": { $lt: 2 } });
3536
});
3637
it("Test operator Less Or Equal To ('=le=')", function () {
37-
assert.equal(rsqlMongoDB('birthday=le="1959-10-21"'), '{ "birthday": { $lte: "1959-10-21" } }');
38-
assert.equal(rsqlMongoDB('birthday=le=1959-10-21'), '{ "birthday": { $lte: new Date("1959-10-21") } }');
39-
assert.equal(rsqlMongoDB('childs=le=2'), '{ "childs": { $lte: 2 } }');
38+
expect(rsqlMongoDB('birthday=le="1959-10-21"')).to.deep.include({ "birthday": { $lte: "1959-10-21" } });
39+
expect(rsqlMongoDB('birthday=le=1959-10-21')).to.be.a('object');
40+
expect(rsqlMongoDB('childs=le=2')).to.deep.include({ "childs": { $lte: 2 } });
4041
});
4142
it("Test operator In ('=in=')", function () {
42-
assert.equal(rsqlMongoDB('childs=in=(1,2,3)'), '{ "childs": { $in: [1,2,3] } }');
43+
expect(rsqlMongoDB('childs=in=(1,2,3)')).to.deep.include({ "childs": { $in: [1,2,3] } });
44+
expect(rsqlMongoDB('childs=in=("1","2","3")')).to.deep.include({ "childs": { $in: ["1","2","3"] } });
4345
});
4446
it("Test operator Out ('=out=')", function () {
45-
assert.equal(rsqlMongoDB('childs=out=(1,2,3)'), '{ "childs": { $nin: [1,2,3] } }');
47+
expect(rsqlMongoDB('childs=out=(1,2,3)')).to.deep.include({ "childs": { $nin: [1,2,3] } });
48+
expect(rsqlMongoDB('childs=out=("1","2","3")')).to.deep.include({ "childs": { $nin: ["1","2","3"] } });
4649
});
4750
it("Test logical operator AND (';')", function () {
48-
assert.equal(rsqlMongoDB('firstName=="john";lastName=="doe"'), '{ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] }');
51+
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });
4952
});
5053
it("Test logical operator OR (',')", function () {
51-
assert.equal(rsqlMongoDB('firstName=="john",firstName=="janne"'), '{ $or: [ { "firstName" : "john" } , { "firstName" : "janne" } ] }');
54+
expect(rsqlMongoDB('firstName=="john",firstName=="janne"')).to.deep.include({ $or: [ { "firstName" : "john" } , { "firstName" : "janne" } ] });
5255
});
5356
it("Test groups", function () {
54-
assert.equal(rsqlMongoDB('(firstName=="john";lastName=="doe"),(firstName=="janne";lastName=="doe")'), '{ $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] }');
57+
expect(rsqlMongoDB('(firstName=="john";lastName=="doe"),(firstName=="janne";lastName=="doe")')).to.deep.include({ $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] });
58+
expect(rsqlMongoDB('(firstName==john;lastName==doe),(firstName=="janne";lastName=="doe")')).to.deep.include({ $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] });
5559
});
5660
it("Test errors", function () {
57-
expect(function () { rsqlMongoDB('azerty') }).to.throw('Wrong RSQL query.');
61+
expect(function () { rsqlMongoDB('azerty') }).to.throw('Wrong RSQL query. No operator found.');
5862
expect(function () { rsqlMongoDB('firstName=={ $where: [ { lastName : "doe" } ] }') }).to.throw('Injection detected.');
5963
});
6064
});

0 commit comments

Comments
 (0)