Add support for logical operator (AND/OR)

This commit is contained in:
Raymond Feng 2014-05-15 08:56:00 -07:00
parent aab66d3131
commit f52e287c82
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) {
if (typeof filter.where === 'function') {
return filter.where;
var where = filter.where;
if (typeof where === 'function') {
return where;
}
var keys = Object.keys(filter.where);
var keys = Object.keys(where);
return function (obj) {
var pass = true;
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;
}
});
@ -350,11 +367,14 @@ function applyFilter(filter) {
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
return value.match(example);
}
if (typeof example === 'undefined') return undefined;
if (typeof value === 'undefined') return undefined;
if (example === undefined || value === undefined) {
return undefined;
}
if (typeof example === 'object') {
// ignore geo near filter
if (example.near) return true;
if (example.near) {
return true;
}
if (example.inq) {
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) {
var remaining = 0;