Merge pull request #112 from strongloop/feature/logical-operator

Add support for logical operator (AND/OR)
This commit is contained in:
Raymond Feng 2014-05-16 08:46:25 -07:00
commit 981185f4c0
2 changed files with 71 additions and 7 deletions

View File

@ -332,14 +332,31 @@ Memory.prototype.all = function all(model, filter, callback) {
}; };
function applyFilter(filter) { function applyFilter(filter) {
if (typeof filter.where === 'function') { var where = filter.where;
return filter.where; if (typeof where === 'function') {
return where;
} }
var keys = Object.keys(filter.where); var keys = Object.keys(where);
return function (obj) { return function (obj) {
var pass = true; var pass = true;
keys.forEach(function (key) { keys.forEach(function (key) {
if (!test(filter.where[key], obj && obj[key])) { if(key === 'and' || key === 'or') {
if(Array.isArray(where[key])) {
if(key === 'and') {
pass = where[key].every(function(cond) {
return applyFilter({where: cond})(obj);
});
return pass;
}
if(key === 'or') {
pass = where[key].some(function(cond) {
return applyFilter({where: cond})(obj);
});
return pass;
}
}
}
if (!test(where[key], obj && obj[key])) {
pass = false; pass = false;
} }
}); });
@ -350,11 +367,14 @@ function applyFilter(filter) {
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') { if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
return value.match(example); return value.match(example);
} }
if (typeof example === 'undefined') return undefined; if (example === undefined || value === undefined) {
if (typeof value === 'undefined') return undefined; return undefined;
}
if (typeof example === 'object') { if (typeof example === 'object') {
// ignore geo near filter // ignore geo near filter
if (example.near) return true; if (example.near) {
return true;
}
if (example.inq) { if (example.inq) {
if (!value) return false; if (!value) return false;

View File

@ -131,6 +131,50 @@ describe('basic-querying', function () {
}); });
}); });
it('should support "and" operator that is satisfied', function (done) {
User.find({where: {and: [
{name: 'John Lennon'},
{role: 'lead'}
]}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 1);
done();
});
});
it('should support "and" operator that is not satisfied', function (done) {
User.find({where: {and: [
{name: 'John Lennon'},
{role: 'member'}
]}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should support "or" that is satisfied', function (done) {
User.find({where: {or: [
{name: 'John Lennon'},
{role: 'lead'}
]}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 2);
done();
});
});
it('should support "or" operator that is not satisfied', function (done) {
User.find({where: {or: [
{name: 'XYZ'},
{role: 'Hello1'}
]}}, function (err, users) {
should.not.exist(err);
users.should.have.property('length', 0);
done();
});
});
it('should only include fields as specified', function (done) { it('should only include fields as specified', function (done) {
var remaining = 0; var remaining = 0;