Chapter 3: Compose

Compose

When we create a Meteor project on our local machines, we have immediate access to a Mongo database that’s created for us. We don’t have to manually setup the database or configure any options. With Meteor’s usual dose of “magic”, everything just works.

When deploying a Meteor project to the web, we can take this same approach. We can let Meteor do all of the work for us

But this isn’t necessarily the best option.

Instead, it can be best to think of the project’s files and database as two separate entities, and rather than hosting them in the same location, host them in two separate locations. We could, for instance, host our project’s files on server with DigitalOcean, and host our project’s database with a service that’s specifically designed to host databases.

Services that are designed to host databases offer a number of advantages over hosting a database alongside your project’s files, including:

  • User-friendly web interfaces for managing the database (similar to phpMyAdmin, but less ugly).
  • The ability to easily scale a database.
  • Automatic backups.

And while it might sound more difficult to host a database separately from the rest of a project, that’s not actually the case. Even for a beginner, deploying an application to the web when the database is hosted on an external service is no more difficult than setting up a database on the server. (The process is only different, not complicated.)

With this in mind, we’re going to do a couple of things…

First, we’ll learn how to host an external database with compose.io — one of the more popular services for hosting an external database. This won’t cost a thing and the benefits of this approach will become obvious.

Second, we’ll learn how to deploy a Meteor application to the web without using an external database. This will provide insight into both options, and ultimately, you can choose whatever method makes the most sense to you.

Either way, all of this is simpler than it may sound.

Introducing, Compose

Compose is the service I generally recommend for hosting external databases, and that’s the case for a number of reasons:

  • The interface is well-designed.
  • Both beginning and advanced developers will find it useful.
  • It’s popular among Meteor developers, so if you ever have any questions about it, there’s a good chance of finding someone with answers.

Other options do exist, like mongolab.com, but once you know how to work with one service, working with the other ones becomes a familiar process.

Getting Started

To begin, register for an account with Compose:

Register at Compose.io

All accounts come with a 30-day trial, but you will be required to enter payment information upon signing up. Once the trial ends, it’ll cost $18 per month for each gigabyte of storage space, but this is only if you want to take your application into production. If you’re just messing around, a completely free option becomes available after registration.

After setting up a new account, delete the default database to avoid being charged once the 30-day trial is over.

To do this:

  1. Click on the “Settings” icon for the default database.
  2. Scroll to the bottom of the page.
  3. Click on the “Delete Deployment” button.

Delete default database

Creating a Database

Once you’ve deleted the default database, the following page will load:

New database options

These are the different types of databases we can host with Compose.

Select the “MongoDB” option and, on the page that loads, click the link that says “Add a Sandbox Database” (it’s on the right side of the screen):

Add a Sandbox Database

Then choose a name for the database:

Choose a database name

…and click the “Add Deployment” button.

When the database has been created:

  1. Click the “Users” icon in the sidebar.
  2. Click the “Add User” button.
  3. Define a username and password for the database, so we can connect to (and manage) it.

Then click the second “Add User” button.

New database options

Mongo URI

After attaching a user to the database, click the “Admin” icon in the sidebar. You’ll be presented with the following two values:

  • Mongo URI
  • Mongo Console

These values allow us to connect to our database in different ways, but the URI value is what we’ll need during the deployment process.

Knowing this:

  1. Copy and paste the Mongo URI value into a text file.
  2. Replace the “user” part with the database’s username.
  3. Replace the “pass” part with the database’s password.

You should have a text file that contains something like this:

mongodb://myusername:mypassword@dogen.mongohq.com:10007/meteor

As long as that’s the case, you’re good to go. You now have a hosted Mongo database that our project can connect to during deployment.