GithubHelp home page GithubHelp logo

peerlibrary / meteor-publish-context Goto Github PK

View Code? Open in Web Editor NEW
8.0 6.0 2.0 7 KB

Provide DDP._CurrentPublish with access to current publish context

Home Page: https://atmospherejs.com/peerlibrary/publish-context

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

JavaScript 53.74% CoffeeScript 46.26%
meteor meteor-package ddp ddp-publish

meteor-publish-context's Introduction

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:

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 package does to provide Meteor.userId().

meteor-publish-context's People

Contributors

mitar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

meteor-publish-context's Issues

It does not work for the automated publications for users

I20180109-18:04:02.592(2)? no invoke, CursorDescription {
I20180109-18:04:02.592(2)?   collectionName: 'users',
I20180109-18:04:02.592(2)?   selector: { _id: 'jzsdfPdbjBMdAedPg' },
I20180109-18:04:02.592(2)?   options:
I20180109-18:04:02.592(2)?    { transform: null,
I20180109-18:04:02.592(2)?      fields: { profile: 1, username: 1, emails: 1 } } }```

When that publication entry is created it does not put it inside DDP._CurrentPublish.get()

Any clues why ?

Publish override might be incomplete

I just had a look at the Meteor.publish override code and it looks like it doesn't take the following format of Meteor.publish into account.

Meteor.publish({
  allPosts() {
    const userId = this.userId;
    return userId ? Posts.find({ userId }) : this.ready();
});

Just as with Meteor.methods, Meteor.publish allows the developer to give it an object.

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.