Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/glynnbird/sqltomango
SQL to Mango (Cloudant Query) JSON converter library
https://github.com/glynnbird/sqltomango
cloudant couchdb mango nodejs sql
Last synced: 2 months ago
JSON representation
SQL to Mango (Cloudant Query) JSON converter library
- Host: GitHub
- URL: https://github.com/glynnbird/sqltomango
- Owner: glynnbird
- License: apache-2.0
- Created: 2017-06-11T11:40:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-08T01:15:14.000Z (over 1 year ago)
- Last Synced: 2024-10-14T11:35:40.689Z (3 months ago)
- Topics: cloudant, couchdb, mango, nodejs, sql
- Language: JavaScript
- Size: 319 KB
- Stars: 28
- Watchers: 8
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqltomango
[![Build Status](https://travis-ci.org/ibm-cds-labs/sqltomango.svg?branch=master)](https://travis-ci.org/ibm-cds-labs/sqltomango) [![npm version](https://badge.fury.io/js/sqltomango.svg)](https://badge.fury.io/js/sqltomango)
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:
```sh
npm install --save sqltomango
```Import the library into your code:
```js
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:
```js
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:
```js
{
"table": "dogs",
"selector": {
"owner": {
"$eq": "glynn"
}
}
}
```This works for more complex queries too:
```js
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