percolate:find-from-publication

v0.1.0Published 10 years ago

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

Find From Publication

Find from publication works around a limitation in Meteor core- there's no way to determine which records, client side, have resulted from a given subscription.

This package works around this issue by tracking the subscription's published document in a special, hidden metadata collection.

API

Server Side

To publish a tracked set of documents, simply call:

1FindFromPublication.publish(name, publisherFn)

This behaves just as Meteor.publish, in that you can call added/removed/changed/ready or simply return (a set of) cursor(s).

Client Side

To get the documents published by a given named publication in a given collection, simply call:

1Collection.findFromPublication(name, query, options);

Example

1var Posts = new Mongo.Collection('posts');
2
3if (Meteor.isServer) {
4  FindFromPublication.publish('allPosts', function() {
5    return Posts.find();
6  });
7} 
8
9if (Meteor.isClient) {
10  Meteor.subscribe('allPosts');
11  
12  // in a helper, etc
13  var postsCursor = Posts.findFromPublication('allPosts');
14}

Sorting

By default, the documents client-side will be sorted by the order they were added.

How it works

When you call added, we simply add a record to the subscriptionMetadata client-only collection. It has the form:

1{
2  _id: 'a-unique-id-for-this-record',
3  collectionName: 'posts',
4  documentId: 'the-real-document-id',
5  publicationName: 'allPosts',
6  rank: 7 // a globally increasing rank over all publications.
7}

License

MIT. (c) Percolate Studio, maintained by Tom Coleman (@tmeasday).

Find From Publication was developed as part of the Verso project.