MapQL (WIP)
A MongoDB inspired ES6 Map() query language. -
This is a WIP; do NOT use in production yet! See TODO for more information.
Testing
Node:
You can test with node.js via npm test
.
Browser:
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.
$ export SAUCE_USERNAME=*****
$ export SAUCE_ACCESS_KEY=*****
$ karma start tests/sauce.karma.js
Browser Support
ES6 supported browsers:
<script src="./dist/MapQL.es6.js"></script>
<script src="./dist/MapQL.es6.chainable.js"></script>
ES5 (babel transpiled):
<script src="./dist/MapQL.es5.js"></script>
<script src="./dist/MapQL.es5.chainable.js"></script>
You can use unpkg to retrieve dist files.
Used with {Instance}.find(<Query>)
and {Instance}.remove(<Query>[, <Multi (Boolean)>])
.
- Comparison
- $eq, $gt, $gte, $lt, $lte, $ne, $in, $nin
- Logical
- Element
- Evaluation
- Array
Used with {Instance}.update(<Query>, <Update>)
.
Example: MapQL.find()
const MapQL = new (require('mapql'))(),
util = require('util'),
_print = (obj) => {
console.log('%s\n', util.inspect(obj, { depth: null, showHidden: true }));
};
MapQL.set('test0', 10);
MapQL.set('test1', 'this is a string');
MapQL.set('test2',{
foo: 7,
bar: 3,
baz: null,
});
MapQL.set('test11',{
foo: 7,
string: 'Look at me example all the things!'
});
MapQL.set('test12',{
foo: 7,
string: 'Another example string!',
baz: 'qux'
});
MapQL.set('test13',{
foo: 8,
baz: 'qux'
});
for (let num = 3; num < 10; num++) {
MapQL.set(`test${num}`, {
foo: Math.floor(Math.random()*15)+1,
bar: Math.floor(Math.random()*15)+1
});
}
_print(MapQL.find({
foo: 8
}));
_print(MapQL.find({
foo: { '$gt': 6 },
bar: { '$lt': 10 }
}));
_print(MapQL.find({
'$gt': 3
}));
_print(MapQL.find({
'$eq': 'this is a string'
}));
_print(MapQL.find({
string: { '$regex': /Things!$/i }
}));
_print(MapQL.find({
'$regex': /String$/i
}));
_print(MapQL.find({
'$and': [{
foo: { '$eq': 7 },
}, {
'$or': [
{ string: { '$regex': /Things!$/i } },
{ string: { '$regex': /String!$/i } },
]
}]
}));
MapQL.findAsync({ foo: { '$gt': 2 }, bar: { '$lt': 10 } }).then((results) => {
_print(results);
}).catch((error) => {
console.log(error);
});
Example: MapQL.chain()
const MapQL = new (require('mapql/chainable'))(),
util = require('util');
7
MapQL.set('testing0', {
foo: 4,
bar: 11
});
MapQL.set('testing1', {
foo: 2,
bar: 9
});
MapQL.set('testing2', {
foo: 8,
bar: 3
});
MapQL.set('testing3', {
foo: 2,
bar: 100
});
let $gt = MapQL.chain().gt('foo', 3);
let $or = MapQL.chain().or((chain) => {
return [
chain.eq('foo', 4),
chain.eq('foo', 2)
]
});
let $and = MapQL.chain().and((chain) => {
return [
chain.lt('foo', 5),
chain.or(() => {
return [
chain.lt('bar', 10),
chain.eq('bar', 100)
];
})
];
});
console.log('$gt query:\n%s\n', util.inspect($gt.query, { depth: null }));
console.log('$gt results:\n%s\n', util.inspect($gt.execute(), { depth: null }));
console.log('$or query:\n%s\n', util.inspect($or.query, { depth: null }));
console.log('$or results:\n%s\n', util.inspect($or.execute(), { depth: null }));
console.log('$and query:\n%s\n', util.inspect($and.query, { depth: null }));
console.log('$and results:\n%s\n', util.inspect($and.execute(), { depth: null }));
console.log('entries:\n%s', util.inspect([...MapQL.entries()], { depth: null }));