https://github.com/peerlibrary/meteor-publish-context
Provide DDP._CurrentPublish with access to current publish context
https://github.com/peerlibrary/meteor-publish-context
ddp ddp-publish meteor meteor-package
Last synced: 2 days ago
JSON representation
Provide DDP._CurrentPublish with access to current publish context
- Host: GitHub
- URL: https://github.com/peerlibrary/meteor-publish-context
- Owner: peerlibrary
- License: bsd-3-clause
- Created: 2017-03-21T09:03:26.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-09-20T02:16:37.000Z (over 5 years ago)
- Last Synced: 2025-04-19T12:42:17.941Z (19 days ago)
- Topics: ddp, ddp-publish, meteor, meteor-package
- Language: JavaScript
- Homepage: https://atmospherejs.com/peerlibrary/publish-context
- Size: 6.84 KB
- Stars: 8
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
`DDP._CurrentPublish` with current publish context
==================================================Adding this package to your [Meteor](http://www.meteor.com/) 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:```javascript
function userId() {
const currentInvocation = DDP._CurrentInvocation.get();
if (currentInvocation) return currentInvocation.userId;const currentContext = DDP._CurrentPublish.get();
if (currentContext) return currentContext.userId;throw new Error("userId() not invoked from a method or publish function.");
}
```This is more or less what [peerlibrary:user-extra](https://github.com/peerlibrary/meteor-user-extra)
package does to provide `Meteor.userId()`.