项目作者: glynnbird

项目描述 :
SQL to Mango (Cloudant Query) JSON converter library
高级语言: JavaScript
项目地址: git://github.com/glynnbird/sqltomango.git
创建时间: 2017-06-11T11:40:24Z
项目社区:https://github.com/glynnbird/sqltomango

开源协议:Apache License 2.0

下载


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:

  1. npm install --save sqltomango

Import the library into your code:

  1. 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:

  1. 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:

  1. {
  2. "table": "dogs",
  3. "selector": {
  4. "owner": {
  5. "$eq": "glynn"
  6. }
  7. }
  8. }

This works for more complex queries too:

  1. 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")
  2. // produces...
  3. {
  4. "table": "dogs",
  5. "fields": [
  6. "_id",
  7. "age",
  8. "breed"
  9. ],
  10. "selector": {
  11. "$or": [
  12. {
  13. "owner": {
  14. "$eq": "glynn"
  15. }
  16. },
  17. {
  18. "$and": [
  19. {
  20. "name": {
  21. "$eq": "towser"
  22. }
  23. },
  24. {
  25. "colour": {
  26. "$eq": "white"
  27. }
  28. }
  29. ]
  30. }
  31. ]
  32. },
  33. "sort": [
  34. {
  35. "age": "desc"
  36. }
  37. ],
  38. "limit": 1500,
  39. "skip": 500
  40. }

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