|
1 | 1 | # ejson-shell-parser |
2 | 2 |
|
3 | | -Parses valid MongoDB EJSON Shell queries. |
4 | | -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. |
5 | | - |
6 | | -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. |
7 | | - |
8 | | -This library currently supports three different modes for parsing queries: |
9 | | - |
10 | | -**strict**: [default] Disallows comments and calling methods |
11 | | - |
12 | | -```javascript |
13 | | -import parse from 'ejson-shell-parser'; |
14 | | - |
15 | | -const query = parse( |
16 | | - `{ |
17 | | - _id: ObjectID("132323"), |
18 | | - simpleCalc: 6, |
19 | | - date: new Date(1578974885017) |
20 | | - }`, |
21 | | - { mode: 'strict' } |
22 | | -); |
23 | | - |
24 | | -/* |
25 | | - query = { _id: ObjectID("132323"), simpleCalc: 6, date: Date('1578974885017') } |
26 | | -*/ |
27 | | -``` |
28 | | - |
29 | | -**weak**: Disallows comments, allows calling methods |
30 | | - |
31 | | -```javascript |
32 | | -import parse from 'ejson-shell-parser'; |
33 | | - |
34 | | -const query = parse( |
35 | | - `{ |
36 | | - _id: ObjectID("132323"), |
37 | | - simpleCalc: Math.max(1,2,3) * Math.min(4,3,2) |
38 | | - }`, |
39 | | - { mode: 'weak' } |
40 | | -); |
41 | | - |
42 | | -/* |
43 | | - query = { _id: ObjectID("132323"), simpleCalc: 6 } |
44 | | -*/ |
45 | | -``` |
46 | | - |
47 | | -**loose**: Supports calling methods on Math, Date and ISODate, allows comments |
48 | | - |
49 | | -```javascript |
50 | | -import parse from 'ejson-shell-parser'; |
51 | | - |
52 | | -const query = parse( |
53 | | - `{ |
54 | | - _id: ObjectID("132323"), // a helpful comment |
55 | | - simpleCalc: Math.max(1,2,3) * Math.min(4,3,2) |
56 | | - }`, |
57 | | - { mode: 'loose' } |
58 | | -); |
59 | | - |
60 | | -/* |
61 | | - query = { _id: ObjectID("132323"), simpleCalc: 6 } |
62 | | -*/ |
63 | | -``` |
64 | | - |
65 | | -The options object passed into parse has the following parameters: |
66 | | - |
67 | | -```javascript |
68 | | -{ |
69 | | - mode: ('loose' || 'weak' || 'strict') // Will assign (allowMethods & allowComments) for you |
70 | | - allowMethods: true, // Allow function calls, ie Date.now(), Math.Max(), (new Date()).getFullYear() |
71 | | - allowComments: true, // Allow comments (// and /* */) |
72 | | -} |
73 | | -``` |
74 | | - |
75 | | -The flags can be set to override the default value from a given mode, ie: |
76 | | - |
77 | | -```javascript |
78 | | -{ |
79 | | - mode: 'strict', |
80 | | - allowComments: true |
81 | | -} |
82 | | -``` |
83 | | - |
84 | | -This options object will disallow method calls, but will allow comments |
| 3 | +> [!IMPORTANT] |
| 4 | +> This package has been moved to https://github.com/mongodb-js/devtools-shared/tree/main/packages/shell-bson-parser |
| 5 | +> and renamed to `@mongodb-js/shell-bson-parser`. |
0 commit comments