Updated recursiveFetch to work with denormalized fields in array links

This commit is contained in:
Berislav 2018-10-25 04:43:53 -07:00
parent 7635337c37
commit 64252de628
5 changed files with 64 additions and 4 deletions

View file

@ -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)));
})
});

View file

@ -15,4 +15,5 @@ if (Meteor.isServer) {
Posts.expose();
Groups.expose();
Authors.expose();
Users.expose();
}

View file

@ -41,6 +41,17 @@ Posts.addLinks({
}
}
}
},
tagsCached: {
collection: Tags,
type: 'many',
field: 'tagIds',
denormalize: {
field: 'tagsCache',
body: {
name: 1
}
}
}
});

View file

@ -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
}
}
},
});

View file

@ -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 () {
})
});
});