All Articles

Useful reusable functions I use in (almost) every Meteor application

Although all the applications I work on are different in their core functionality, there are always some useful functions that I reuse in almost every Meteor application.

I try my best to stick to the DRY (don’t repeat yourself) principle when I’m building applications. There are certain checks that tend to get used in most of my Meteor.method()s, and using the ES6 modules it’s really easy to write them as a module and import them in to other files that need to use them.

Today I’m going to share some of the functions I use in (almost) every Meteor application.

User Roles Checks

Most of the applications I work on rely on the alanning:roles package. This package allows you to attach certain roles to your users (e.g. Admin, Subscriber, Revoked) which you can test against in your methods/publications. The following modules are really useful for checking a user’s roles in a Meteor.method().

path: /imports/modules/both/isAdmin.js

import { Meteor } from "meteor/meteor";
import { Roles } from "meteor/alanning:roles";

const isAdmin = (userId) => {
  if (!Roles.userIsInRole(userId, "administrator")) {
    throw new Meteor.Error(
      "unauthorised",
      "You are not authorised to do this."
    );
  }
};

export default isAdmin;

Use it in a method like this:

import { Meteor } from "meteor/meteor";
import { check } from "meteor/check";
import isAdmin from "/imports/modules/both/is-user-admin";

Meteor.methods({
  "User.Delete"(userId) {
    check(userId, String);

    isAdmin(this.userId);

    return Meteor.users.remove(userId);
  },
});

If the currently logged in user who is calling the method doesn’t have the role of ‘administrator’, the module will throw an error and stop the method from running.

import { Meteor } from "meteor/meteor";

const isUserLoggedIn = (userId) => {
  if (!userId) {
    throw new Meteor.Error(
      "unauthorised",
      "You need to be logged in to do this."
    );
  }
  return;
};

export default isUserLoggedIn;

Similar to the isAdmin() module, the isUserLoggedIn() module checks if there is a user logged in and throws an error if not.

Date Formatting

I frequently need to work with Dates. One of the first npm packages I install in almost all of my applications is the fantastic moment.js. This library makes working with dates significantly easier, and I have built a number of reusable date functions based on it. Here are a few examples:

import moment from "moment";

export const formatDate = (date) => {
  return moment(date).format("DD/MM/YYYY");
  // => 30/08/2017
};

export const formatDateHoursMins = (date) => {
  return moment(date).format("DD/MM/YYYY - HH:mm");
  // => 30/08/2017 - 12:00
};

export const formatDateReadable = (date) => {
  return moment(date).format("ddd Do MMM YYYY");
  // => Wednesday 30th August 2017
};

Use This Pattern For Any Reusable Function

This pattern works really well for any function that you need in more than one place. I have a /imports/modules directory with a both, client and server directory in it for different modules that are needed in different places. I recommend you use this pattern in your applications to reduce the vertical space of methods/publications and to stop yourself from typing the same functionality over and over again.