Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated docs #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 55 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,48 @@ $ mrt add publish-with-relations

## API

### Basics
### How to use it
Let's say you want to render a page that requires data from three collections: Posts, Comments, and Users.

```javascript
// Post JSON
"post" : {
"_id" : [objectId],
"authorId" : [objectId]
}
```

```javascript
// Comment JSON
"comment" : {
"_id" : [objectId],
"userId" : [objectId],
"postId" : [postId],
"approved" : [Boolean]
}
```

```javascript
// User JSON
"user" : {
"_id" : [objectId]
}
```

The publish-with-relations package will allow you to publish all three under a single publication/subscription.

For example, the publication below will return the post (specified by the id parameter), along with the user profile of the auther and 10 approved comments with their author profiles as well.

```javascript
// On the client
if( Meteor.isClient() ){
Meteor.subscribe('post', postId);
}
```

```javascript
// On the server
if(Meteor.isServer()){
Meteor.publish('post', function(id) {
Meteor.publishWithRelations({
handle: this,
Expand All @@ -41,18 +80,25 @@ $ mrt add publish-with-relations
}]
});
});
}
```
What you'll notice is that you can actually nest mappings/relationships within one another. So for example, a post has many comments, and each comment has an author.

### Properties of Meteor.publishWithRelations
```collection``` (Collection)
Add the name of the collection

```key``` (String)
This is the foreign key that relates two collections. For example, a Post has a User through ```authorId```, so the key would be ```authorId```.

This will publish the post specified by id parameter together
with user profile of its author and a list of ten approved comments
with their author profiles as well.
```reverse``` (Boolean, [false])
Reverse tells the relationship of the key between the collections. Reverse should be set to true if the foreign key is a property of the mapped Collection. For example, ```postId``` is a property of Comments, which is mapped to Posts. Therefore, reverse should be true.

With one call we publish a post to the ```Posts``` collection, post
comments to the ```Comments``` collection and corresponding authors to
the ```Meteor.users``` collection so we have all the data we need to
display a post.
```filter``` (Object)
The selectors for a Meteor mongo query

You can check another (more complex) example at this [gist](https://gist.github.com/erundook/5012259).
```options``` (Object)
Options on the Meteor mongo query

### Changelog
#### v0.1.4
Expand Down