Add support for matching array values à la mongo.

This commit is contained in:
Laurent Villeneuve 2015-08-27 23:32:53 -04:00
parent 0b22e2a2fb
commit 632898b022
2 changed files with 36 additions and 6 deletions

View File

@ -416,7 +416,20 @@ function applyFilter(filter) {
}
}
if (test(where[key], getValue(obj, key))) {
var value = getValue(obj, key);
// Support referencesMany and other embedded relations
// Also support array types. Mongo, possibly PostgreSQL
if (Array.isArray(value)) {
var matcher = where[key];
return value.some(function (v, i) {
var filter = {where: {}};
filter.where[i] = matcher;
return applyFilter(filter)(value);
});
}
if (test(where[key], value)) {
return true;
}
@ -428,7 +441,7 @@ function applyFilter(filter) {
var subFilter = {where: {}};
var subKey = key.substring(dotIndex+1);
subFilter.where[subKey] = where[key];
return !!subValue.filter(applyFilter(subFilter)).length
return subValue.some(applyFilter(subFilter));
}
return false;

View File

@ -134,7 +134,6 @@ describe('Memory connector', function() {
});
});
// describe.only('Query for memory connector', function() {
describe('Query for memory connector', function() {
var ds = new DataSource({
connector: 'memory'
@ -310,6 +309,22 @@ describe('Memory connector', function() {
});
});
it('should successfully extract 2 users matching array values', function (done) {
User.find({
where: {
'children': {
regexp: /an/
}
}
}, function (err, users) {
should.not.exist(err);
users.length.should.be.equal(2);
users[0].name.should.be.equal('John Lennon');
users[1].name.should.be.equal('George Harrison');
done();
});
});
it('should count using date string', function(done) {
User.count({birthday: {lt: new Date(1990,0).toISOString()}},
function(err, count) {
@ -492,7 +507,8 @@ describe('Memory connector', function() {
{ name: 'Paul McCartney' },
{ name: 'George Harrison' },
{ name: 'Ringo Starr' },
]
],
children: ['Sean', 'Julian']
},
{
seq: 1,
@ -512,9 +528,10 @@ describe('Memory connector', function() {
{ name: 'John Lennon' },
{ name: 'George Harrison' },
{ name: 'Ringo Starr' },
]
],
children: ['Stella', 'Mary', 'Heather', 'Beatrice', 'James']
},
{seq: 2, name: 'George Harrison', order: 5, vip: false},
{seq: 2, name: 'George Harrison', order: 5, vip: false, children: ['Dhani']},
{seq: 3, name: 'Ringo Starr', order: 6, vip: false},
{seq: 4, name: 'Pete Best', order: 4},
{seq: 5, name: 'Stuart Sutcliffe', order: 3, vip: true}