2.3.0 • Published 3 years ago

mongodb-language-model v2.3.0

Weekly downloads
1,777
License
SSPL
Repository
github
Last release
3 years ago

mongodb-language-model

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:

npm.io

Installation

npm install --save mongodb-language-model

Testing

npm test
2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

2.0.0

3 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.2.0

7 years ago

1.0.2

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

1.4.2

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

6 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.2.3

9 years ago

0.3.1

9 years ago

0.2.2

9 years ago

0.3.4

8 years ago

0.3.3

9 years ago