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:
1import { FindFromPublication } from 'meteor/percolate:find-from-publication'; 2 3FindFromPublication.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);
or
1Collection.findOneFromPublication(name, query, options);
Example
1import { Meteor } from 'meteor/meteor'; 2import { Mongo } from 'meteor/mongo'; 3import { FindFromPublication } from 'meteor/percolate:find-from-publication'; 4 5const Posts = new Mongo.Collection('posts'); 6 7if (Meteor.isServer) { 8 FindFromPublication.publish('allPosts', function() { 9 return Posts.find(); 10 }); 11} 12 13if (Meteor.isClient) { 14 Meteor.subscribe('allPosts'); 15 16 // in a helper, etc 17 const postsCursor = Posts.findFromPublication('allPosts'); 18 const randomPost = Posts.findOneFromPublication('allPosts', { _id: 'x45ebOafda' }); 19}
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.