Giter Club home page Giter Club logo

rsql-mongodb's Introduction

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

rsql-mongodb's People

Contributors

fizcko avatar zishone avatar 195858 avatar subash25 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.