dburles:reactive-relations

v0.1.7Published 10 years ago

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

reactive-relations

Intelligent reactive relational publications for Meteor

This package is no longer under development.

Please instead use reywood:publish-composite

What is it?

Publishing reactive relationships is not a trivial task in Meteor. What reactive-relations does is expose an API that allows you to publish and subscribe to reactive relationships easily.

There are a couple of other solutions to this problem but what's different here is that we're not setting up observers server-side (like publish-with-relations) so we're not taxing the server. The client passes the required values automatically and in doing so, keeps everything in sync.

For more information on what exactly this package is solving (and how) please see this wiki page

API Examples

Define these between client and server

It's important that both client and server can see these, so make sure they go in a file that's visible by both.

1// Publish a simple news feed
2// where our feed document looks a little like:
3// {
4//   "_id" : "vhMPxubr8rTdaLvj4"
5//   "body" : "hi",
6//   "createdAt" : 1391649092926,
7//   "userId" : "vcaEodnAHahveESaE",
8//   "eventId" : "2uRsvYj8f9ZaQHHSa"
9// }
10
11Reactive = {
12  feed: {
13    cursor: function() { return Feeds.find({}, { limit: 20, sort: { createdAt: -1 }}); },
14    relations: [{
15      collection: function() { return Meteor.users; },
16      parentKey: 'userId'
17    }, {
18      collection: function() { return Events; },
19      parentKey: 'eventId'
20    }]
21  }
22};
1// Both do the same thing
2Reactive = {
3  authorsWithBooks: {
4    cursor: function() { return Authors.find(); },
5    relations: [{
6      collection: function() { return Books; },
7      key: 'authorId',
8      filter: {},
9      options: {}
10    }]
11  },
12  booksWithAuthors: {
13    cursor: function() { return Books.find(); },
14    relations: [{
15      collection: function() { return Authors; },
16      parentKey: 'authorId',
17      filter: {},
18      options: {}
19    }]
20  }
21};
22

Publishing and Subscribing

On the server simply write:

1Meteor.publishReactive('feed');

and on the client:

1Meteor.subscribeReactive('feed');

Licence

MIT