# ejson-shell-parser

> Parses valid MongoDB EJSON Shell queries. This library does not validate that these queries are correct. It's focus is on parsing untrusted input. You may wish to use something like https://github.com/mongodb-js/mongodb-language-model to achieve this.

Latest version **2.0.1** (published 2024-02-20) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ejson-shell-parser
pnpm add ejson-shell-parser
yarn add ejson-shell-parser
bun add ejson-shell-parser
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2024-02-20 |
| First published | 2020-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 1 |
| Unpacked size | 542.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Scott Sidwell |
| Maintainers | orechova, himanshusinghs, c-buckingham, mongo-j, mabaasit, alexander_schroll, chuck.kalmanek, mongodb-js-user, rueckstiess, durran, lerouxb, fredtruman, mbroadst, hswolff, satyasinha, matt_d_rat, rhysm, tomhollander, alena.khineika, jeff-allen-mongo, mmarcon, jonathan.balsano, mongodb-build, jack.weir, stennie, mcasimir, kristina.stefano, jarjee, shaketbaby, devtoolsbot, addaleax, gribnoysup, mutukrish, scottsidwell |

## Links

- npm: https://www.npmjs.com/package/ejson-shell-parser
- npm.io page: https://npm.io/package/ejson-shell-parser

## Dependencies (1)

- [acorn](https://npm.io/package/acorn.md) ^8.1.0

## Recent versions

- 2.0.1 (latest) — 2024-02-20
- 2.0.0 — 2023-10-16
- 1.2.4 — 2023-05-03
- 1.2.3 — 2023-05-03
- 1.2.2 — 2023-02-22
- 1.2.1 — 2023-02-09
- 1.2.0 — 2022-11-01
- 1.1.4 — 2022-10-17
- 1.1.3 — 2022-04-29
- 1.1.2 — 2022-04-29
- 1.1.1 — 2021-02-16
- 1.1.0 — 2021-02-16
- 1.0.2 — 2020-09-10
- 1.0.1 — 2020-09-10
- 1.0.0 — 2020-09-10
- … 2 more at https://npm.io/package/ejson-shell-parser/versions

## README

# ejson-shell-parser

Parses valid MongoDB EJSON Shell queries.
This library does not validate that these queries are correct. It's focus is on parsing untrusted input. You may wish to use something like https://github.com/mongodb-js/mongodb-language-model to achieve this.

This library creates an AST from the proposed input, and then traverses this AST to check if it looks like a valid MongoDB query. If it does, the library will then evaluate the code to produce the parsed query.

This library currently supports three different modes for parsing queries:

**strict**: [default] Disallows comments and calling methods

```javascript
import parse from 'ejson-shell-parser';

const query = parse(
  `{
    _id: ObjectID("132323"),
    simpleCalc: 6,
    date: new Date(1578974885017)
  }`,
  { mode: 'strict' }
);

/*
  query = { _id: ObjectID("132323"), simpleCalc: 6, date: Date('1578974885017') }
*/
```

**weak**: Disallows comments, allows calling methods

```javascript
import parse from 'ejson-shell-parser';

const query = parse(
  `{
    _id: ObjectID("132323"),
    simpleCalc: Math.max(1,2,3) * Math.min(4,3,2)
  }`,
  { mode: 'weak' }
);

/*
  query = { _id: ObjectID("132323"), simpleCalc: 6 }
*/
```

**loose**: Supports calling methods on Math, Date and ISODate, allows comments

```javascript
import parse from 'ejson-shell-parser';

const query = parse(
  `{
    _id: ObjectID("132323"), // a helpful comment
    simpleCalc: Math.max(1,2,3) * Math.min(4,3,2)
  }`,
  { mode: 'loose' }
);

/*
  query = { _id: ObjectID("132323"), simpleCalc: 6 }
*/
```

The options object passed into parse has the following parameters:

```javascript
{
  mode: ('loose' || 'weak' || 'strict') // Will assign (allowMethods & allowComments) for you
  allowMethods: true, // Allow function calls, ie Date.now(), Math.Max(), (new Date()).getFullYear()
  allowComments: true, // Allow comments (// and /* */)
}
```

The flags can be set to override the default value from a given mode, ie:

```javascript
{
  mode: 'strict',
  allowComments: true
}
```

This options object will disallow method calls, but will allow comments

---
_Source: https://npm.io/package/ejson-shell-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
