rsql-mongodb
TypeScript icon, indicating that this package has built-in type declarations

2.2.0 • Public • Published

rsql-mongodb

Converting RSQL queries to MongoDB queries.

NPM Version Build Coverage

Installation

$ npm install rsql-mongodb

What is RSQL

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.

Basic operators supported

  • 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=

Logical operators supported

  • AND : ;
  • OR : ,

Additionals operators

  • Like (Regex) : =regex= (to match regex values)
  • Exists : =exists= (to check if property exists)
NOTE

Parenthesized expression can be used to define the precedence.

Return values

Return an Object or null.
This object can be passed to mongoDB methods find(), findOne(), ...

Examples

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" } ] } ] }

    // Using "_id"
    rsqlMongoDB('_id==650a7389a7ab39ddcfbc6832');
    //=> { "_id" : new ObjectId('650a7389a7ab39ddcfbc6832') }

    // Escape special character "(" ")" ";" and ","
    rsqlMongoDB('lastName=="janne\\(doe\\)"')
    //=> { "lastName" : "janne(doe)" }
    rsqlMongoDB('lastName=="janne\\;doe"')
    //=> { "lastName" : "janne;doe" }
}
catch(err){
    console.log(err);
}

License

MIT

Package Sidebar

Install

npm i rsql-mongodb

Weekly Downloads

331

Version

2.2.0

License

MIT

Unpacked Size

15.2 kB

Total Files

5

Last publish

Collaborators

  • fizcko