Merge pull request #710 from WyzeLink/feature/support-implicit-array-matching

Add support for matching array values à la mongo.
This commit is contained in:
Raymond Feng 2015-08-31 09:14:15 -07:00
commit e91b5cfa9c
2 changed files with 49 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,35 @@ describe('Memory connector', function() {
});
});
it('should successfully extract 2 users matching over 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 successfully extract 1 users matching over array values', function (done) {
User.find({
where: {
children: 'Dhani'
}
}, function (err, users) {
should.not.exist(err);
users.length.should.be.equal(1);
users[0].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 +520,8 @@ describe('Memory connector', function() {
{ name: 'Paul McCartney' },
{ name: 'George Harrison' },
{ name: 'Ringo Starr' },
]
],
children: ['Sean', 'Julian']
},
{
seq: 1,
@ -512,9 +541,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}