peerlibrary:publish-context

v0.7.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.

DDP._CurrentPublish with current publish context

Adding this package to your Meteor application creates a DDP._CurrentPublish environment variable which you can use to access current publish context (this inside publish function) from anywhere in the server-side code, if that server-side code was called from a publish function.

It is similar to existing undocumented DDP._CurrentInvocation which does the same for method calls, giving you access to the current context of a method call (this inside a method's body).

Not needed with Meteor >= 1.5.1. There is DDP._CurrentPublicationInvocation now available, and DDP._CurrentInvocation has been renamed to DDP._CurrentMethodInvocation. This package provides the same functionality for previous Meteor versions.

Server side only.

Installation

meteor add peerlibrary:publish-context

After adding the package, you have to depend on, or import, Meteor's core ddp package to get access to the DDP symbol.

Examples

Implementing a userId() function on the server-side, which works in any code called from methods or publish functions, can then be simply implemented as:

1function userId() {
2  const currentInvocation = DDP._CurrentInvocation.get();
3  if (currentInvocation) return currentInvocation.userId;
4
5  const currentContext = DDP._CurrentPublish.get();
6  if (currentContext) return currentContext.userId;
7
8  throw new Error("userId() not invoked from a method or publish function.");
9}

This is more or less what peerlibrary:user-extra package does to provide Meteor.userId().