tmeasday:publish-with-relations

v0.2.0Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Publish with relations

Publish with relations builds on Tom's gist to provide a convenient way to publish associated records.

Installation

Publish with relations can be installed with Meteorite. From inside a Meteorite-managed app:

$ mrt add publish-with-relations

API

Basics

1  Meteor.publish('post', function(id) {
2    Meteor.publishWithRelations({
3      handle: this,
4      collection: Posts,
5      filter: id,
6      mappings: [{
7        key: 'authorId',
8        collection: Meteor.users
9      }, {
10        reverse: true,
11        key: 'postId',
12        collection: Comments,
13        filter: { approved: true },
14        options: {
15          limit: 10,
16          sort: { createdAt: -1 }
17        },
18        mappings: [{
19          key: 'userId',
20          collection: Meteor.users
21        }]
22      }]
23    });
24  });

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.

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.

You can check another (more complex) example at this gist.

Changelog

v0.1.4

  • Foreign key can be an array now. Thanks Tom!

Compatibility notes

Use <= v0.1.1 for meteor version < 0.5.7