A MongoDB inspired ES6 Map() query language. -
This is a WIP; do NOT use in production yet! See TODO for more information.
You can test with node.js via npm test
.
For 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.
See karma-sauce-launcher and sauce.karma.js.
# See more about Open Source testing at https://saucelabs.com/open-source
$ export SAUCE_USERNAME=***** # Your Sauce Labs username.
$ export SAUCE_ACCESS_KEY=***** # Your Sauce Labs access key.
$ karma start tests/sauce.karma.js # npm run-script sauce
<!-- MapQL() WITHOUT chain() support (es6)-->
<script src="./dist/MapQL.es6.js"></script>
<!-- MapQL() WITH chain() support (es6) -->
<script src="./dist/MapQL.es6.chainable.js"></script>
<!-- MapQL() WITHOUT chain() support-->
<script src="./dist/MapQL.es5.js"></script>
<!-- MapQL() WITH chain() support (es6) -->
<script src="./dist/MapQL.es5.chainable.js"></script>
You can use unpkg to retrieve dist files.
Search 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.
#find({ '<Field>.<Field>.<Field>': { <Query Operators> } });
// Cursor [ Document { _id: 'foo', value: { bar: { baz: 1 } } } ]
{Instance}.find({ 'bar.baz': { $eq: 1 } });
The Promise
based version of #find()
. More information to come.
The 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.
Specifies 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.
Use the limit() method on a cursor to specify the maximum number of documents the cursor will return.
The 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
).
// Cursor [ Document { _id: 'Foo', value: 'Bar' } ]
{Instance}.set('Foo', 'Bar')
The $eq
operator matches documents where the value of a field equals (==
) the specified value. This is also the default find()
operator.
<value> OR { <field>: { $eq: <value> } }
The following examples query against MapQL
with the following documents:
{Instance}.set('foo1', 'bar');
{Instance}.set('foo2', { bar: 'baz' });
{Instance}.set('bar', 'foo3');
The 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
.
// Cursor [ Document { _id: 'foo2', value: { bar: 'baz' } } ]
{Instance}.find({ bar: { $eq: 'baz' } });
// Cursor [ Document { _id: 'foo1', value: 'bar' } ]
{Instance}.find({ $eq: 'bar' });
/*
Cursor [
Document { _id: 'bar', value: 'foo3', [Symbol(bykey)]: true },
Document { _id: 'foo1', value: 'bar', [Symbol(bykey)]: false },
]
*/
{Instance}.find('bar');
The $gt
operator selects those documents where the value is greater than (>
) the specified value.
The $gte
operator selects the documents where the value of the field is greater than or equal to (>=
) the specified value.
The $lt
operator selects the documents where the value of the field is less than (<
) the specified value.
The $lte
operator selects the documents where the value of the field is less than or equal to (<=
) the specified value.
The $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.
The $in
operator selects the documents where the value of a field equals any value in the specified Array
.
The $nin
operator selects the documents where the field value is not in the specified Array
or the field does not exist.
$or -- Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
The $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
.
$and -- Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
The $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
.
When 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.
The $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.
Provides regular expression capabilities for pattern matching strings in queries. MapQL()
uses JavaScript regular expressions (RegExp).
Use 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
.
The $size
operator matches any Array
with the number of elements specified by the argument.
The $set
operator sets or replaces the value of a field with the specified value.
The $inc
operator increments a field by a specified value.
The $mul
operator multiplies the value of a field by a number.
The $unset
operator deletes a particular field.
The $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
.
Please note that importing and exporting data is highly experimental. This feature currently exports as json, so certain keys or references may not be supported.
As 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
of Map()
would be greatly appreciated. Please see #5 for further information.
Current (known) supported data types:
- Primitives
- Boolean, Null, Undefined, Number, String, Symbol, Object
- Extended support
- Array, Function, Date, Map, Set, Buffer/Uint8Array, RegExp, NaN
- Experimental support
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 event 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(), this is an experimental (under tested) feature of MapQL. See TypedArray for more information about typed arrays.