UNPKG

11.1 kBMarkdownView Raw
1MapQL (WIP)
2===
3A __[MongoDB]__ inspired __[ES6 Map()]__ query language. - [![Build Status](https://travis-ci.org/LouisT/MapQL.svg?branch=dev)](https://travis-ci.org/LouisT/MapQL)
4
5This is a ___WIP___; do __NOT__ use in production yet! See [TODO](TODO.md) for more information.
6
7Testing
8-
9#### Node:
10You can test with node.js via `npm test`.
11
12#### Browser:
13For local browser testing with [karma] run `npm run-script local` or `karma start tests/local.karma.js` to run tests with [PhantomJS] and/or any browsers you have access to.
14
15#### [Sauce Labs]:
16See [karma-sauce-launcher] and [sauce.karma.js](tests/sauce.karma.js).
17
18```sh
19 # See more about Open Source testing at https://saucelabs.com/open-source
20 $ export SAUCE_USERNAME=***** # Your Sauce Labs username.
21 $ export SAUCE_ACCESS_KEY=***** # Your Sauce Labs access key.
22 $ karma start tests/sauce.karma.js # npm run-script sauce
23```
24
25Browser Support
26-
27[![Build Status](https://saucelabs.com/browser-matrix/louist-mapql.svg)](https://saucelabs.com/u/louist-mapql)
28
29### ES6 supported browsers:
30```html
31<!-- MapQL() WITHOUT chain() support (es6)-->
32<script src="./dist/MapQL.es6.js"></script>
33<!-- MapQL() WITH chain() support (es6) -->
34<script src="./dist/MapQL.es6.chainable.js"></script>
35````
36
37### ES5 (babel transpiled):
38```html
39<!-- MapQL() WITHOUT chain() support-->
40<script src="./dist/MapQL.es5.js"></script>
41<!-- MapQL() WITH chain() support (es6) -->
42<script src="./dist/MapQL.es5.chainable.js"></script>
43```
44You can use [unpkg] to retrieve dist files.
45
46### `#find(<Query>)`
47Search the `MapQL()` with the supplied query operators. The result is a `Cursor` with matching `Document` objects. Entries that have an `Object` as the value can be searched with `dot notation` fields.
48###### `Dot notation`
49```javascript
50#find({ '<Field>.<Field>.<Field>': { <Query Operators> } });
51```
52###### Example:
53```javascript
54// Cursor [ Document { _id: 'foo', value: { bar: { baz: 1 } } } ]
55{Instance}.find({ 'bar.baz': { $eq: 1 } });
56```
57### `#findAsync(<Query Operators>)`
58The `Promise` based version of `#find()`. More information to come.
59
60#### Cursor
61The `Cursor` class is an extended `Array` containing `Document` objects found with `find()`, providing `sort()` and `limit()` methods along with the rest of the [Array methods]. Please note that calling `limit()` _BEFORE_ `sort()` will limit the number of documents _BEFORE_ sorting and vise versa.
62
63##### `#sort(<Sort>)` -- Returns results ordered according to a sort specification.
64Specifies the order in which the query returns matching documents. Specify in the sort parameter the field or fields to sort by and a value of `1` or `-1` to specify an ascending or descending sort respectively.
65
66##### `#limit(<Limit>)` -- Constrains the size of a cursor's result set.
67Use the limit() method on a cursor to specify the maximum number of documents the cursor will return.
68
69#### Document
70The `Document` class is an extended `Object` containing the `MapQL()` entry within `Cursor`. The `Document` consits of an `_id` (entry `key`) and a `value` (entry `value`).
71```javascript
72// Cursor [ Document { _id: 'Foo', value: 'Bar' } ]
73{Instance}.set('Foo', 'Bar')
74```
75
76Implemented Query Operators
77-
78### Comparison
79##### $eq -- Matches values that are equal to a specified value.
80The `$eq` operator matches documents where the value of a field equals (`==`) the specified value. This is also the default `find()` operator.
81```
82<value> OR { <field>: { $eq: <value> } }
83```
84The following examples query against `MapQL` with the following documents:
85```javascript
86{Instance}.set('foo1', 'bar');
87{Instance}.set('foo2', { bar: 'baz' });
88{Instance}.set('bar', 'foo3');
89```
90The following example queries the `MapQL` instance to select all documents where the value equals `{ bar: 'baz' }` or `bar`. If a `Document` is added to the `Cursor` by it's key value, `Symbol(bykey)` will be true in the `Document`.
91```javascript
92// Cursor [ Document { _id: 'foo2', value: { bar: 'baz' } } ]
93{Instance}.find({ bar: { $eq: 'baz' } });
94// Cursor [ Document { _id: 'foo1', value: 'bar' } ]
95{Instance}.find({ $eq: 'bar' });
96/*
97Cursor [
98 Document { _id: 'bar', value: 'foo3', [Symbol(bykey)]: true },
99 Document { _id: 'foo1', value: 'bar', [Symbol(bykey)]: false },
100]
101*/
102{Instance}.find('bar');
103```
104
105##### $gt -- Matches values that are greater than a specified value.
106The `$gt` operator selects those documents where the value is greater than (`>`) the specified value.
107
108##### $gte -- Matches values that are greater than or equal to a specified value.
109The `$gte` operator selects the documents where the value of the field is greater than or equal to (`>=`) the specified value.
110
111##### $lt -- Matches values that are less than a specified value.
112The `$lt` operator selects the documents where the value of the field is less than (`<`) the specified value.
113
114##### $lte -- Matches values that are less than or equal to a specified value.
115The `$lte` operator selects the documents where the value of the field is less than or equal to (`<=`) the specified value.
116
117##### $ne -- Matches all values that are not equal to a specified value.
118The `$ne` operator selects the documents where the value of the field is not equal (`!=`) to the specified value. This includes documents that do not contain the field.
119
120##### $in -- Matches any of the values specified in an array.
121The `$in` operator selects the documents where the value of a field equals any value in the specified `Array`.
122
123##### $nin -- Matches none of the values specified in an array.
124The `$nin` operator selects the documents where the field value is not in the specified `Array` or the field does not exist.
125
126#### Logical
127##### $or -- Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
128The `$or` operator performs a logical OR operation on an `Array` of two or more `expressions` and selects the documents that satisfy at least one of the `expressions`.
129
130##### $and -- Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
131The `$and` operator performs a logical AND operation on an `Array` of two or more expressions (e.g. `expression1`, `expression2`, etc.) and selects the documents that satisfy all the expressions in the `Array`.
132
133#### Element
134##### $exists -- Matches documents that have the specified field.
135When `boolean` is `true`, `$exists` matches the documents that contain the field, including documents where the field value is `null`. If `boolean` is `false`, the query returns only the documents that do not contain the field.
136
137##### $type -- Selects documents if a field is of the specified type.
138The `$type` operator selects the documents where the value of the field is an instance of the specified data type. Querying by data type is useful when dealing with highly unstructured data where data types are not predictable.
139
140#### Evaluation
141##### $regex -- Selects documents where values match a specified regular expression.
142Provides regular expression capabilities for pattern matching strings in queries. `MapQL()` uses JavaScript regular expressions ([RegExp]).
143
144##### $where -- Matches documents that satisfy a JavaScript expression.
145Use the `$where` operator to pass a JavaScript function to the query system. The `$where` provides greater flexibility, but requires that the `MapQL()` processes the function for each document. Reference the document in the function using either `this` or as the first function `argument`.
146
147#### Array
148##### $size -- Selects documents if the array field is a specified size.
149The `$size` operator matches any `Array` with the number of elements specified by the argument.
150
151Implemented Update Operators
152-
153#### Fields
154##### $set -- Sets the value of a field in a document.
155The `$set` operator sets or replaces the value of a field with the specified value.
156
157##### $inc -- Increments the value of the field by the specified amount.
158The $inc operator increments a field by a specified value.
159
160##### $mul -- Multiplies the value of the field by the specified amount.
161Multiply the value of a field by a number.
162
163##### $unset -- Removes the specified field from a document.
164The $unset operator deletes a particular field.
165
166#### Array
167##### $pop -- Removes the first or last item of an array.
168The `$pop` operator removes the first or last element of an `Array`. Pass `$pop` a value of `-1` to remove the first element of an `Array` and `1` to remove the last element in an `Array`.
169
170Import/Export
171-
172Please note that importing and exporting data is highly experimental. This feature currently exports as json, so certain keys or references may not be supported.
173As well, due to the way `Symbol()` works, it's impossible to export any key/value pairs from Objects if the key is a Symbol. Any input on how to improve import/export
174of `Map()` would be greatly appreciated. Please see #5 for further information.
175
176Current (known) supported [data types](/src/DataTypes.js):
177* [Primitives]
178 * Boolean, Null, Undefined, Number, String, Symbol, Object
179* Extended support
180 * Array, Function, Date, Map, Set, [Buffer]/[Uint8Array], RegExp, NaN
181* Experimental support
182 * [TypedArray]
183
184[Buffer] and [Uint8Array] are similar enough that browsers will attempt to convert a Buffer into a Uint8Array if the Buffer constructor is unavailable. In the unlikely
185event that nether are available, import() will then attempt to convert it to a normal [Array] with Array.from(). Typed arrays are tested with [ArrayBuffer.isView()],
186this is an experimental (under tested) feature of MapQL. See [TypedArray] for more information about typed arrays.
187
188
189[MongoDB]: https://www.mongodb.com/
190[ES6 Map()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
191[Classes]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
192[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
193[Arrow]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
194[unpkg]: https://unpkg.com/mapql/
195[karma]: http://karma-runner.github.io/
196[karma-sauce-launcher]: https://github.com/karma-runner/karma-sauce-launcher
197[Sauce Labs]: https://saucelabs.com/
198[PhantomJS]: http://phantomjs.org/
199[Primitives]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types
200[Buffer]: https://nodejs.org/api/buffer.html
201[Uint8Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
202[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
203[Array methods]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods
204[Array.from]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
205[ArrayBuffer.isView()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView
206[TypedArray]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray