From 632898b02272a010076d65ea440d88f949bc09bc Mon Sep 17 00:00:00 2001 From: Laurent Villeneuve Date: Thu, 27 Aug 2015 23:32:53 -0400 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20matching=20array=20values?= =?UTF-8?q?=20=C3=A0=20la=20mongo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/connectors/memory.js | 17 +++++++++++++++-- test/memory.test.js | 25 +++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) 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..fb6f655c 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,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}