From 64252de628e8384158e84074e1f60d7bd49ab3fe Mon Sep 17 00:00:00 2001 From: Berislav Date: Thu, 25 Oct 2018 04:43:53 -0700 Subject: [PATCH] Updated recursiveFetch to work with denormalized fields in array links --- lib/query/lib/recursiveFetch.js | 8 +++-- lib/query/testing/bootstrap/index.js | 1 + lib/query/testing/bootstrap/posts/links.js | 11 +++++++ lib/query/testing/bootstrap/users/links.js | 13 +++++++- lib/query/testing/client.test.js | 35 +++++++++++++++++++++- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/query/lib/recursiveFetch.js b/lib/query/lib/recursiveFetch.js index 8c2ff29..015f91d 100755 --- a/lib/query/lib/recursiveFetch.js +++ b/lib/query/lib/recursiveFetch.js @@ -55,8 +55,12 @@ function fetch(node, parentObject, fetchOptions = {}) { * - pass node.results to accessor above and find with sift */ - const currentIds = _.pluck(collectionNode.results, '_id'); - collectionNode.results.push(...collectionNodeResults.filter(res => !_.contains(currentIds, res._id))); + collectionNode.results.push(...collectionNodeResults); + + // this was not working because all references must be replaced in snapBackCaches, not only the ones that are + // found first + // const currentIds = _.pluck(collectionNode.results, '_id'); + // collectionNode.results.push(...collectionNodeResults.filter(res => !_.contains(currentIds, res._id))); }) }); diff --git a/lib/query/testing/bootstrap/index.js b/lib/query/testing/bootstrap/index.js index 24b3cbe..81c1bad 100755 --- a/lib/query/testing/bootstrap/index.js +++ b/lib/query/testing/bootstrap/index.js @@ -15,4 +15,5 @@ if (Meteor.isServer) { Posts.expose(); Groups.expose(); Authors.expose(); + Users.expose(); } diff --git a/lib/query/testing/bootstrap/posts/links.js b/lib/query/testing/bootstrap/posts/links.js index f0e28ec..ac6d2aa 100755 --- a/lib/query/testing/bootstrap/posts/links.js +++ b/lib/query/testing/bootstrap/posts/links.js @@ -41,6 +41,17 @@ Posts.addLinks({ } } } + }, + tagsCached: { + collection: Tags, + type: 'many', + field: 'tagIds', + denormalize: { + field: 'tagsCache', + body: { + name: 1 + } + } } }); diff --git a/lib/query/testing/bootstrap/users/links.js b/lib/query/testing/bootstrap/users/links.js index 9533da2..cd3c431 100755 --- a/lib/query/testing/bootstrap/users/links.js +++ b/lib/query/testing/bootstrap/users/links.js @@ -10,5 +10,16 @@ Users.addLinks({ collection: Users, field: 'subordinateIds', type: 'many' - } + }, + friendsCached: { + collection: Users, + field: 'friendIds', + type: 'many', + denormalize: { + field: 'friendsCache', + body: { + name: 1 + } + } +}, }); diff --git a/lib/query/testing/client.test.js b/lib/query/testing/client.test.js index e51f2c0..6778118 100755 --- a/lib/query/testing/client.test.js +++ b/lib/query/testing/client.test.js @@ -270,7 +270,40 @@ describe('Query Client Tests', function () { assert.isFunction(clone.setParams({}).fetchOne); }); + it('Should work with denormalized fields in the many links', async () => { + const query = createQuery({ + users: { + $filters: { + name: {$regex: 'User - (2|3)'} + }, + friends: { + name: 1, + friendsCached: { + name: 1 + } + } + } + }); + + const handle = query.subscribe(); + await waitForHandleToBeReady(handle); + + const users = query.fetch(); + + assert.equal(users.length, 2); + users.forEach(user => { + user.friends.forEach(friend => { + // each friend should have defined friendsCached + assert.isArray(friend.friendsCached); + // db cache field should be removed + assert.isUndefined(friend.friendCache); + + friend.friendsCached.forEach(cache => assert.isString(cache.name)); + }); + }); + }); + it('Should work securely with reactive queries and linked exposures', function () { - }) + }); });