Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass subscription argument? #6

Open
DeanKamali opened this issue Jul 27, 2019 · 2 comments
Open

How to pass subscription argument? #6

DeanKamali opened this issue Jul 27, 2019 · 2 comments

Comments

@DeanKamali
Copy link

checking the examples on here, couldn't figure out how I can pass subscription argument to pipelineCreator function.

Example: trying to pass userId

Meteor.subscribe('message.counts.by.sender', userId);

I have tried the following but it doesn't work

const pipelineCreator = (userId) => ([
  { $match: { recipient_id: { $eq: userId) } } },
  { $group: { _id: { sender_id: '$sender_id' }, count: { $sum: 1 }, sender_name: { $first: '$sender_name' } } },
]);

Any ideas?

@icellan
Copy link

icellan commented Mar 20, 2020

Disclaimer: I am not the creator of this package, but I imagine you could do something like:

import { Meteor } from 'meteor/meteor';
import { buildAggregator } from 'meteor/lamoglia:publish-aggregation';
import { Messages } from '../../messages/messages.js';

Meteor.publish('message.counts.by.sender', function(userId) {
  const pipelineCreator = (userId) => ([
    { $match: { recipient_id: { $eq: userId) } } },
    { $group: { _id: { sender_id: '$sender_id' }, count: { $sum: 1 }, sender_name: { $first: '$sender_name' } } },
  ]);

  const options = {
    collectionName: 'messagecounts',
    transform: (doc) => ({ ...doc, sender_id: doc._id.sender_id }),
    singleValueField: 'count',
    pastPeriod: {
      millis: 24 * 60 * 60 * 1000,
      field: 'sent_at',
    },
  };

  buildAggregator(Invoices, pipelineCreator, options).call(this);
});

The idea is just to call wrap the buildAggregator in a function in which you can run any code you want, and then call the buildAggregator within your function, passing the context along.

EDIT: I got it working in a test environment on my end, using code very similar to above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@DeanKamali @icellan and others