Converting RSQL queries to MongoDB queries.
$ npm install rsql-mongodb
RSQL (RESTful Service Query Language) is based on FIQL (Feed Item Query Language).
It's a query language that introduces basic and logical operators. This is perfect for RESTful APIs.
- Equal to : ==
- Not equal to : !=
- Less than : =lt=
- Less than or equal to : =le=
- Greater than : =gt=
- Greater than or equal to : =ge=
- In : =in=
- Not in : =out=
- AND : ;
- OR : ,
- Like (Regex) : =regex= (to match regex values)
- Exists : =exists= (to check if property exists)
Parenthesized expression can be used to define the precedence.
Return an Object or null.
This object can be passed to mongoDB methods find(), findOne(), ...
const rsqlMongoDB = require('rsql-mongodb');
try{
// String comparison : you can add quotes to force string values
rsqlMongoDB('lastName=="doe"');
//=> { "lastName" : "doe" }
rsqlMongoDB('lastName==janne');
//=> { "lastName" : "janne" }
// Boolean comparison
rsqlMongoDB('married!=true');
//=> { "married": { $ne: true } }
// Number comparison
rsqlMongoDB('childs=gt=2');
//=> { "childs": { $gt: 2 } }
// Date comparison
rsqlMongoDB('birthday=ge=1959-10-21');
//=> { "birthday": { $gte: new Date("1959-10-21T00:00:00.000Z") } }
// In comparison
rsqlMongoDB('childs=in=(1,2,3)');
//=> { "childs": { $in: [1,2,3] } }
// Out comparison
rsqlMongoDB('childs=out=(1,2,3)');
//=> { "childs": { $nin: [1,2,3] } }
// Like operator
rsqlMongoDB('lastName=regex=do*');
//=> { "lastName": { $regex: "do*", $options: "" } }
// Like operator with options
rsqlMongoDB('lastName=regex=do*=si');
//=> { "lastName": { $regex: "do*", $options: "si" } }
rsqlMongoDB('lastName=regex="do=*"=si');
//=> { "lastName": { $regex: "do=*", $options: "si" } }
// 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" } ] } ] }
}
catch(err){
console.log(err);
}