is there a javascript library that will allow me to express object predicates in a DSL similar to MongoDB's query language? for the sake of clarity in a large program, i'd like to be able to say:
var obj = {
a: 1,
b: 'abcdefg'
}, qry = {
a: { $gt: 0 },
b: /^abc/
};
if(query(qry).matches(obj)) {
// do something appropriate since
}
instead of:
var obj = {
a: 1,
b: 'abcdefg'
};
if(obj.a>0 && qry.b.test(obj.b)) {
// do something appropriate
}
i'm using Node.js, so anything on NPM would be great; it would be an added bonus if the library can select objects out of an array as well as just matching individual objects.
i reviewed these two related questions, but they weren't particularly helpful for my situation:
OK I found the answer: Sift.js
Now for the long answer: This has been asked and answered before. The salient points are:
filter() for example.As a final note, mongodb-riff appears to be trying to do something similar but currently the page states clearly that it doesn't work - perhaps it's abandoned. But his readme is at least of value :-), he mentions sift and Query Engine which looks more mature, though too complicated for me!
Personally I'm going to go with Underscore because now that I've looked into it for the first time, I realise that it has heaps of handy stuff I need, plus I really only wanted to do simple functions like what would be _.find() in Underscore. But I guess if you want to do more complicated mongo-like queries, you'll do it in less LOC with Sift.
Previous (now irrelevant) answer:
Sorry for this partially useless answer, but there definitely is one that does exactly what you want. I saw it the other day on Github and now I want to use it so I'm searching for it again... but I came to your question instead of the library. But when I find it I'll update this answer.
Check out Mingo
I implemented it after finding no suitable alternative.
It is still actively being developed but is usable. Test coverage is not complete.
Usable from both browser and nodejs
[Edit]
This library is now the most complete implementation of MongoDB's query language for the frontend.
You want to use lodash.js. Its a drop in replacement for underscore.js. The performance is twice as fast.
The closest I could find was linq.js, which is LINQ for JavaScript. Hopefully this will be of some help to you.
https://github.com/mirek/node-json-criteria library does exactly that - evaluates criteria queries on JSON objects using MongoDB query format.