Code Monkey home page Code Monkey logo

sqltomango's Introduction

sqltomango

Build Status npm version

A simple Node.js library that converts Structured Query Language (SQL) into CouchDB Mango / Cloudant Query JSON objects. Mango is the code name for the query language used in Apache CouchDB and IBM Cloudant. It uses JSON to represent queries. This tool converts SQL strings into Mango objects, to allow users to interact with CouchDB/Cloudant database with SQL queries.

Installation

Use sqltomango in your project with:

npm install --save sqltomango

Import the library into your code:

var sqltomango = require('sqltomango');

Usage

The sqltomango library is a single function that accepts one argument - a string containing the SQL you wish to convert to a query object:

var q = sqltomango.parse("SELECT * FROM dogs WHERE owner = 'glynn'")

It returns an object which can be used to to query a CouchDB or Cloudant database:

{
  "table": "dogs",
  "selector": {
    "owner": {
    "$eq": "glynn"
    }
  }
}

This works for more complex queries too:

var q = sqltomango.parse("SELECT _id, age, breed FROM dogs WHERE owner = 'glynn' OR (name='towser' AND colour='white') ORDER BY age DESC LIMIT 500,1500")
// produces...
{
  "table": "dogs",
  "fields": [
    "_id",
    "age",
    "breed"
  ],
  "selector": {
    "$or": [
      {
        "owner": {
          "$eq": "glynn"
        }
      },
      {
        "$and": [
          {
            "name": {
              "$eq": "towser"
            }
          },
          {
            "colour": {
              "$eq": "white"
            }
          }
        ]
      }
    ]
  },
  "sort": [
    {
      "age": "desc"
    }
  ],
  "limit": 1500,
  "skip": 500
}

Errors

An exception is thrown if the SQL does not parse or contains SQL features not supported by Mango including:

  • GROUP BY
  • SUM/COUNT/AVG/DISTINCT
  • UNION
  • JOIN

sqltomango's People

Contributors

glynnbird avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.