Skip to content

Commit

Permalink
Define children as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
nabiltntn committed Apr 6, 2018
1 parent 26c73e1 commit 1c65da0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
33 changes: 31 additions & 2 deletions README.md
Expand Up @@ -36,9 +36,11 @@ Arguments

A function that returns a MongoDB cursor (e.g., `return Meteor.users.find({ active: true });`)

* **`children`** -- *array (optional)*
* **`children`** -- *array (optional)* or *function*

An array containing any number of object literals with this same structure
- An array containing any number of object literals with this same structure
- A function with top level documents as arguments. It helps dynamically build
the array based on conditions ( like documents fields values)

* **`collectionName`** -- *string (optional)*

Expand Down Expand Up @@ -85,6 +87,33 @@ Arguments
}
```

Example with children as function:

```javascript
{
find() {
return Notifications.find();
},
children(parentNotification) {
// children is a function that returns an array of objects.
// It takes parent documents as arguments and dynamically builds children array.
if (parentNotification.type === 'about_post') {
return [{
find(notification) {
return Posts.find(parentNotification.objectId);
}
}];
}
return [
{
find(notification) {
return Comments.find(parentNotification.objectId);
}
}
]
}
}
```

## Examples

Expand Down
6 changes: 4 additions & 2 deletions lib/publication.js
Expand Up @@ -10,7 +10,7 @@ class Publication {
constructor(subscription, options, args) {
check(options, {
find: Function,
children: Match.Optional([Object]),
children: Match.Optional(Match.OneOf([Object], Function)),
collectionName: Match.Optional(String),
});

Expand Down Expand Up @@ -91,7 +91,9 @@ class Publication {
}

_publishChildrenOf(doc) {
_.each(this.childrenOptions, function createChildPublication(options) {
const children = _.isFunction(this.childrenOptions) ?
this.childrenOptions(doc, ...this.args) : this.childrenOptions;
_.each(children, function createChildPublication(options) {
const pub = new Publication(this.subscription, options, [doc].concat(this.args));
this.publishedDocs.addChildPub(doc._id, pub);
pub.publish();
Expand Down
29 changes: 29 additions & 0 deletions tests/client.js
Expand Up @@ -105,6 +105,21 @@ describe('publishComposite', () => {
},
});

testPublication('Should publish all post comment authors with children as Function', {
publication: 'allPostsWithChildrenAsFunction',

testHandler: (onComplete) => {
const comments = Comments.find();

comments.forEach((comment) => {
const commentAuthor = Authors.findOne({ username: comment.author });
asyncExpect(() => expect(commentAuthor).to.be.defined, onComplete);
});

onComplete();
},
});

testPublication('Should publish one user\'s posts', {
publication: 'userPosts',
args: ['marie'],
Expand Down Expand Up @@ -316,6 +331,20 @@ describe('publishComposite', () => {
},
});

testPublication('Should publish authors to both Authors with children as Function with Multip leLevel', {
publication: 'publishCommentAuthorsWithChildrenAsFunctionMultipleLevel',

testHandler: (onComplete) => {
const marieAsAuthor = Authors.findOne({ username: 'marie' });
const stephenAsCommentAuthor = CommentAuthors.findOne({ username: 'stephen' });

asyncExpect(() => expect(marieAsAuthor).to.be.defined, onComplete);
asyncExpect(() => expect(stephenAsCommentAuthor).to.not.be.defined, onComplete);

onComplete();
},
});

testPublication('Should publish two top level publications specified with a function', {
publication: 'twoUsersPosts',
args: ['marie', 'albert'],
Expand Down
35 changes: 35 additions & 0 deletions tests/server.js
Expand Up @@ -39,6 +39,17 @@ publishComposite('allPosts', {
children: postPublicationChildren,
});

publishComposite('allPostsWithChildrenAsFunction', {
find() {
return Posts.find();
},
children: parentPost => (parentPost.author === 'albert' ? [{
find(post) {
return Authors.find({ username: post.author });
},
}] : postPublicationChildren),
});

publishComposite('userPosts', username => ({
find() {
debugLog('userPosts', 'userPosts.find() called');
Expand Down Expand Up @@ -97,6 +108,30 @@ publishComposite('publishCommentAuthorsInAltClientCollection', {
],
});

publishComposite('publishCommentAuthorsWithChildrenAsFunctionMultipleLevel', {
find() {
return Posts.find();
},
children: [
{
find(post) {
return Authors.find({ username: post.author });
},
},
{
find(post) {
return Comments.find({ postId: post._id });
},
children: (parentComment, parentPost) => (parentComment.author === 'richard' ? [{
collectionName: 'commentAuthors',
find(comment) {
return Authors.find({ username: comment.author });
},
}] : []),
},
],
});

publishComposite('twoUsersPosts', (username1, username2) => [
{
find() {
Expand Down

0 comments on commit 1c65da0

Please sign in to comment.