JSPM

mongoloidsql

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 31
  • Score
    100M100P100Q60620F
  • License MIT

Build a SQL 'where' from a mongo selector

Package Exports

  • mongoloidsql

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongoloidsql) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status

Mongo selector SQL builder (WIP)

Build an SQL selector (WHERE) from a Mongo selector.

NOTE: Currently the produced SQL is mysql opinionated

Install

npm install mongoloidsql

Usage

Example 1

var builder = require('mongoloidsql');

console.log('SELECT * FROM `table` WHERE ' + builder({id: 1});

Output:

SELECT * FROM `table` WHERE `id` = 1

Example 2

var builder = require('mongoloidsql');

console.log('SELECT * FROM `table` WHERE ' + builder({id: 1, 'test' : { '$gt': 1}});

Output:

SELECT * FROM `table` WHERE `id` = 1 AND `test` > 1

etc...

what what

var builder = require('mongoloidsql');

console.log('SELECT * FROM `table` WHERE ' + builder({
        'col1': 1,
        "$and": [
          {
            $or: [
              {'col2': 2},
              {'col3': 3}
            ]
          },
          {
            'col4': {
              '$eq': 4
            }
          },
          {
            'col5': {
              '$ne': 5
            }
          },
          {
            '$and': [
              {'col6': {'$gt': 6}},
              {'col7': {'$lte': 7}},
              {'$or': [
                {'col8': {'$gt': 6}},
                {'col9': {'$lte': 7}}
              ]}
            ]
          }
        ],
        '$or': [
          {
            $or: [
              {'col2': 2},
              {'col3': 3}
            ]
          },
          {
            'col4': {
              '$eq': 4
            }
          },
          {
            'col5': {
              '$ne': 5
            }
          },
          {
            '$and': [
              {'col6': {'$gt': 6}},
              {'col7': {'$lte': 7}},
              {'$or': [
                {'col8': {'$gt': 6}},
                {'col9': {'$lte': 7}}
              ]}
            ]
          }
        ],
      })
    );
  }));

Output:

SELECT * FROM `table` WHERE (`col1` = 1 AND ((`col2` = 2 OR `col3` = 3) AND `col4` = 4 AND `col5` != 5 AND (`col6` > 6 AND `col7` <= 7 AND (`col8` > 6 OR `col9` <= 7))) AND ((`col2` = 2 OR `col3` = 3) OR `col4` = 4 OR `col5` != 5 OR (`col6` > 6 AND `col7` <= 7 AND (`col8` > 6 OR `col9` <= 7))))

Reference

What is not (yet) implemented/supported