query.js is an abstract query language which can be mapped to different query language implementations. It runs in the browser as well as on Node.js.
On Node.js include the query module.
var query = require("query").query;
var myQuery = query().eql("foo",3);
For use in the browser include the query.js file.
<script src="http://github.com/lib/query.js" type="text/javascript"></script>
To create a new query instance you have two options:
var query = query();
var query2 = new Query();
Sorting ascending by name:
var q = query()
.sort("name");
Sorting descending by name:
var q = query()
.sort("-name");
Sorting over more than one field (short written):
var q = query()
.sort("name", "-firstname", "+birthday");
Sorting over more than one field (long written):
var q = query()
.sort([{
field: "xyz"
}, {
field: "abc",
direction: -1
}]);
Comparison operators are by default concatinated with ANDs.
var q = query()
.eql("name", "tony")
.gt("age", 18);
var q = query()
.not(function (where) {
where
.eql("name", "tony")
});
var q = query()
.or(function () {
this.eql("name", "tony")
}, function () {
this.eql("name", "marcus")
});
var q = query()
.and(function () {
this.eql("name", "tony")
}, function () {
this.eql("name", "marcus")
});
The ArrayExecutor allows you to execute the query on an array of items.
var items = [
{
name: "Adam",
age: 18,
address: {
city: "New York"
}
},
{
name: "Bob",
age: 20
},
{
name: "Charlie",
age: 10
}
];
var q = query().eql("name", "Adam");
var filterItems = ArrayExecutor.filterItems(q,items);
/* output: [{
name: "Adam",
age: 18,
address: {
city: "New York"
}
}] */
- REST query parser - parses a REST query string to a query object
The project includes the following query composers:
- REST query composer - composes the query to a REST query string
- MongoDB query composer - composes the query to a MongoDB JSON query format