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

Compatibility with percolate:find-from-publication #98

Closed
SimonSimCity opened this issue Mar 9, 2017 · 4 comments
Closed

Compatibility with percolate:find-from-publication #98

SimonSimCity opened this issue Mar 9, 2017 · 4 comments

Comments

@SimonSimCity
Copy link
Member

SimonSimCity commented Mar 9, 2017

I tried to wrap my head around how I could combine this library with the functionality of percolate:find-from-publication.

The task of this plugin is to recognize, which publication was responsible for publishing an item - and also query items depending on which publication now published it to the client. In many cases, this is very convenient, since you now don't have to query another collection.

If this plugin would have a way to only return an object instead of a cursor at a find() method, I guess, would already be enough. I'd then create a new child-subscription and only return a self-created object which should be published to the METADATA_COLLECTION collection on the client.

I hope you see what I want to archive 😉

It's mostly not to push items to a separate collection, but to identify the publication that published it.

By building a custom object for METADATA_COLLECTION, I could also work around the limitation, described here: http://braindump.io/meteor/2014/09/20/publishing-to-an-alternative-clientside-collection-in-meteor.html#comment-2211775496

This is quite related to #28 ...

@reywood
Copy link
Collaborator

reywood commented Mar 12, 2017

Feel free to fork and modify this package however you see fit.

@reywood reywood closed this as completed Mar 12, 2017
@SimonSimCity
Copy link
Member Author

SimonSimCity commented Apr 4, 2017

EDIT: please my latter comment. The solution proposed there might be better suited for your case.

If anyone wonders:

I haven't written a PR, but just implemented in the way I wanted it to be.

Take a look at this sample to get an idea of how it could work out for you 😉

Server:

Meteor.publishComposite('posts', (selectors, filters, linkCollectionName) => {
  const definition = {
    find() {
      return Posts.find(selectors, filters);
    },
    children: [
      {
        find(post) {
          return Comments.find({ postId: post._id });
        },
      },
    ],
  };

  // If you want, put the ids only into a separate client-side collection
  if (linkCollectionName) {
    definition.children.push({
      find(posts) {
        return Posts.find(post._id, { fields: { _id: 1 } });
      },
      collectionName: linkCollectionName,
    });
  }

  return definition;
});

Client:

const Linker = new Mongo.Collection('client.posts-linker');

Meteor.subscribe('posts', {}, {}, 'client.posts-linker');

Posts.find({
  _id: { $in: Linker.find({}).map((el) => el._id) },
});

@erencay
Copy link

erencay commented May 16, 2017

Thanks @SimonSimCity
Here is a more generic version if needed by anyone;

Client

const PublicationsTracker = new Mongo.Collection('client.publicationsTracker');

Meteor.subscribe('users');

Users.find({
  _id: { $in: PublicationsTracker.find({ publication: 'users' }).map((el) => el._id) },
});

Server

return {
    find() {
      return Meteor.users.find(selectors, filters);
    },

    children: [{
      find(user) {
        this.added("client.publicationsTracker", user._id, { publication: 'users' });
        this.ready();
      },
    }],
  }

@SimonSimCity
Copy link
Member Author

SimonSimCity commented Sep 20, 2017

The solution I suggested here is known to be very slow on low-power devices (tablets and phones), because you have to join the data on the client side - it seems quite Ok on a desktop PC though.

Please take a look at this feature request and see if my work-around isn't suited and maybe faster for your use-case: meteor/meteor-feature-requests#183

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