Coder Social home page Coder Social logo

sql2mongo's Introduction

sql2mongo Build Status npm

Use SQL syntax to query MongoDB

Installation

npm install sql2mongo

Usage

getMongoQuery

Parses an SQL WHERE clause to a mongo query object.

Example:

const { getMongoQuery } = require("sql2mongo");

const mongoQuery = getMongoQuery(`
  show = "Friends" OR
  (city = "New York" AND
  year BETWEEN 2005 AND 2014 AND
  name IN ("Ted", "Marshall", "Barney"))
`)

Result:

{
  $or: [
    { show: "Friends" },
    {
      city: "New York",
      year: { $gt: 2005, $lt: 2014 },
      name: { $in: ["Ted", "Marshall", "Barney"] },
    }
  ]
}

Nested objects

If you need to query a nested object, use the NESTED function inside your query. The NESTED function receives a string with a WHERE clause for the nested object.

Example: If you would like to query the following nested document:

{
  item: 'journal',
  qty: 25,
  size: { h: 14, w: 21, uom: 'cm' },
  status: 'A'
}

You can use the NESTED function like this:

const { getMongoQuery } = require("sql2mongo");

const mongoQuery = getMongoQuery(`
  size = NESTED("h = 14 AND w > 20")
`)

Result:

{
  size: {
    h: 14,
    w: { $gt: 20 }
  }
}

Otherwise, you can use the dot annotation:

const { getMongoQuery } = require("sql2mongo");

const mongoQuery = getMongoQuery(`
  size.h = 14 AND 
  size.w > 20
`)

Result:

{
  "size.h": 14,
  "size.w": { $gt: 20 }
}

TODO

  • Add support for all DML commands (insert, update, etc.)

Build

  • Run npm run build to build the package.

LICENSE

MIT

sql2mongo's People

Contributors

dependabot[bot] avatar hogpilot avatar orgoldfus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sql2mongo's Issues

Null expression type is not supported

The expression: getMongoQuery('field IN (NULL, 3)')
fails with: Uncaught Error: Unsupported expression type: Null

However, using the js-sql-parser library, it does parse to valid AST:
sqlParser.parse('SELECT * FROM table WHERE field IN (NULL, 3)')

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.