All Projects → mongodb-js → mongodb-language-model

mongodb-js / mongodb-language-model

Licence: Apache-2.0 license
Parses MongoDB query language and creates hierarchical Ampersand.js models to interact with the query tree

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mongodb-language-model

electron-license
Tools for electron apps to work with licenses.
Stars: ✭ 18 (-30.77%)
Mutual labels:  compass-tools
mongodb-query-exporter
Prometheus MongoDB aggregation query exporter
Stars: ✭ 74 (+184.62%)
Mutual labels:  mongodb-query
ampersand-router
Clientside router with fallbacks for browsers that don't support pushState. Mostly lifted from Backbone.js.
Stars: ✭ 69 (+165.38%)
Mutual labels:  ampersand
ucast
Conditions query translator for everything
Stars: ✭ 76 (+192.31%)
Mutual labels:  mongodb-query
query-parser
Safe parsing and validation for MongoDB queries (filters), projections, and more.
Stars: ✭ 28 (+7.69%)
Mutual labels:  compass-tools
mongodb-schema
Infer a probabilistic schema for a MongoDB collection.
Stars: ✭ 106 (+307.69%)
Mutual labels:  compass-tools

mongodb-language-model

build status

Parses a MongoDB query and creates an abstract syntax tree (AST) with part of speech tagging. Currently, only strict extended json syntax is supported (which means keys have to be surrounded by double quotes and values have to match the strict syntax of the extended-json format.);

Usage

The main module exposes two functions: accepts(queryStr) and parse(queryStr).

accepts(queryStr)

The accepts(queryStr) function takes a query string and returns true if the string is a valid MongoDB query, false otherwise.

Example:

var accepts = require('mongodb-language-model').accepts;
var assert = require('assert');

assert.ok(accepts('{"foo": 1}'));
assert.ok(accepts('{"age": {"$gt": 35}}'));
assert.ok(accepts('{"$or": [{"email": {"$exists": true}}, {"phone": {"$exists": true}}]}'));

assert.equal(accepts('{"$invalid": "key"}'), false);

parse(queryStr)

The parse(queryStr) function takes a query string and returns an abstract syntax tree (AST) as a javascript object, if the query is valid. If the query is not valid, the function throws a pegjs.SyntaxError with a message explaining the failure.

Example:

var parse = require('mongodb-language-model').parse;
var assert = require('assert');
var pegjs = require('pegjs');

var ast = parse('{"foo": "bar"}');
assert.deepEqual(ast, {
  'pos': 'expression',
  'clauses': [
    {
      'pos': 'leaf-clause',
      'key': 'foo',
      'value': {
        'pos': 'leaf-value',
        'value': 'bar'
      }
    }
  ]
});

UML diagram

This is the hierarchical model that is created when a query is parsed:

Installation

npm install --save mongodb-language-model

Testing

npm test

License

Apache 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].