https://github.com/peerlibrary/meteor-subscription-scope
Scope queries on collections to subscriptions
https://github.com/peerlibrary/meteor-subscription-scope
ddp ddp-subscription meteor meteor-package
Last synced: 5 days ago
JSON representation
Scope queries on collections to subscriptions
- Host: GitHub
- URL: https://github.com/peerlibrary/meteor-subscription-scope
- Owner: peerlibrary
- License: bsd-3-clause
- Created: 2015-12-23T10:30:57.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-20T01:44:10.000Z (over 5 years ago)
- Last Synced: 2025-03-31T01:03:35.967Z (25 days ago)
- Topics: ddp, ddp-subscription, meteor, meteor-package
- Language: CoffeeScript
- Homepage: https://atmospherejs.com/peerlibrary/subscription-scope
- Size: 10.7 KB
- Stars: 21
- Watchers: 7
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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](http://www.meteor.com/) 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`](http://docs.meteor.com/#/full/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](http://docs.meteor.com/#/full/meteor_publish) function `this` is
extended with:* `enableScope()` – when enabled, for subscriptions to this publish endpoint, clients can use `scopeQuery()`
to limit queries only to the subscriptionExamples
--------If on the server side you have such publish endpoint (using
[MongoDB full-text search](https://docs.mongodb.org/v2.6/reference/operator/query/text/)):```javascript
Meteor.publish('search-documents', function (search) {
this.enableScope();var query = {$text: {$search: search}};
query['score_' + this._subscriptionId] = {$meta: 'textScore'};return MyCollection.find(query);
});
```Then you can on the client side subscribe to it and query only the documents returned from it:
```javascript
var subscription = Meteor.subscribe('search-documents', 'foobar');var sort = {}
sort['score_' + subscription.subscriptionId] = -1;// Returns documents found on the server, sorted by the full-text score.
MyCollection.find(subscription.scopeQuery(), {sort: sort}).fetch();// Returns count of documents found on the server authored by the current user.
MyCollection.find({$and: [subscription.scopeQuery(), {author: Meteor.userId()}]}).count();
```Related projects
----------------* [find-from-publication](https://github.com/versolearning/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