GithubHelp home page GithubHelp logo

isabella232 / mongodb-language-model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mongodb-js/mongodb-language-model

0.0 0.0 0.0 1.04 MB

Parses MongoDB query language and creates hierarchical Ampersand.js models to interact with the query tree

License: Apache License 2.0

JavaScript 100.00%

mongodb-language-model's Introduction

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

mongodb-language-model's People

Contributors

alenakhineika avatar dependabot-preview[bot] avatar durran avatar imlucas avatar matt-d-rat avatar pzrq avatar rueckstiess avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.