diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 2857d8e7..ee7f2864 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -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; diff --git a/test/memory.test.js b/test/memory.test.js index 07b0e17b..c16c2ea1 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -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}