Giter Site home page Giter Site logo

shaunstoltz / meteor-subscription-scope Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peerlibrary/meteor-subscription-scope

0.0 1.0 0.0 12 KB

Scope queries on collections to subscriptions

Home Page: https://atmospherejs.com/peerlibrary/subscription-scope

License: BSD 3-Clause "New" or "Revised" License

CoffeeScript 90.32% JavaScript 9.68%

meteor-subscription-scope's Introduction

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):

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:

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 – 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

meteor-subscription-scope's People

Contributors

mitar avatar matheusccastroo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.