peerlibrary:subscription-scope

v0.5.0Published 5 years ago

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

subscription scope

This Meteor smart package allows scoping of queries on collections only to documents published by a subscription.

Adding this package to your Meteor application extends subscription's handle with scopeQuery and publish endpoint function's this is extended with this.enableScope().

Both client and server side.

Installation

meteor add peerlibrary:subscription-scope

API

The subscription handle returned from Meteor.subscribe contain a new method:

  • scopeQuery() – returns a query which limits collection's documents only to this subscription

Limiting is only done on documents, not on fields. If multiple publish endpoints publish different fields and you subscribe to them, all combined fields will still be available in all queries on the client side.

Inside the publish endpoint function this is extended with:

  • enableScope() – when enabled, for subscriptions to this publish endpoint, clients can use scopeQuery() to limit queries only to the subscription

Examples

If on the server side you have such publish endpoint (using MongoDB full-text search):

1Meteor.publish('search-documents', function (search) {
2  this.enableScope();
3
4  var query = {$text: {$search: search}};
5  query['score_' + this._subscriptionId] = {$meta: 'textScore'};
6
7  return MyCollection.find(query);
8});

Then you can on the client side subscribe to it and query only the documents returned from it:

1var subscription = Meteor.subscribe('search-documents', 'foobar');
2
3var sort = {}
4sort['score_' + subscription.subscriptionId] = -1;
5
6// Returns documents found on the server, sorted by the full-text score.
7MyCollection.find(subscription.scopeQuery(), {sort: sort}).fetch();
8
9// Returns count of documents found on the server authored by the current user.
10MyCollection.find({$and: [subscription.scopeQuery(), {author: Meteor.userId()}]}).count();
  • find-from-publication – uses an extra collection which means that there are some syncing issues between collections and much more data is send to the client for every document; in short, it is much more complicated solution to the simple but powerful approach used by this package