Methods and subscriptions as collection helpers

Most of my meteor methods and publications belong to one of these two categories:

  1. They are specific to a collection (e.g. publication myPosts, method newPost are specific to the collection Posts).
  2. They are specific to an element (e.g. publication postComments, method newComment are specific to a post).

This calls for two types of helpers:

  1. Collection helpers like Posts.myPost(), Posts.newPost()
  2. Element helpers (or just helpers), like post.postComments(), post.newComment().

You might already use dburles:collection-helpers. This post builds on top of that package.

What we want are 4 methods registerCollectionActions, registerActions, registerCollectionCursors, registerCursors that allow us to register methods and publications as helpers:

registerCollectionCursors(Posts, {
myPosts({userId}, {limit}) {
return Posts.find({
}, {
registerCollectionActions(Posts, {
newPost({title, content}) {
if (Meteor.userId()) {
userId: Meteor.userId()
registerCursors(Post, {
comments() {
return Comments.find({
postId: this._id
registerActions(Posts, {
newComment({content}) {
if (Meteor.userId()) {
userId: Meteor.userId(),
postId: this._id,

and then use them like

// A collection cursor helper
const posts = Posts.myPosts();
// A collection action helper
title: "Hello world",
content: "Lorem ipsum",
// A post cursor helper
const comments = post.comments();
// A post action helper
content: "Hi there"

Note that:

  1. Action helpers are based on methods, that is they are simulated on the client and executed on the server. You can add a callback as a last argument to the helper.
  2. Cursor helpers are based on publications, therefore are reactive. They take care of subscribing to the underlying publications, with the method described in my last post.

If you want these functions, look no further: they are available in this gist (this gist is also needed). Leave a comment to let me know what you think :)

If you want to know how I use these collection helpers inside of react components, stay tuned, that’s the subject of my next article.

Like what you read? Give Nick Redmark a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.