How to set up Sinatra with ActiveRecord
In my series Learning Docker, I document my steps to use Sinatra with Docker. Part of this journey is to set up Sinatra with a database, and because I love ActiveRecord I want to use it as well. In this post, I am going to show you how to do that.
In case you are not familiar with my series and its goals, I’ll quickly give you an overview:
For one of my projects I decided to take a look at Docker, and build my application using services. Most of these I want to build as JSON APIs with Sinatra, since I like how lightweight yet extensible it is. The first parts of the series dealt mostly with setting up a very basic Sinatra app and getting Docker to run.
If you want to inspect the source code of the application as it is now, follow this link: firstname.lastname@example.org
Pulling in the dependencies
Let’s start by adding the gem
pg to our
Gemfile. This is the database
adapter for PostgreSQL, which are are going to use in production.
We are also adding
sqlite3 for the development and test environment.
Next, we need to configure our application. As I mentioned, I am a big fan of ActiveRecord, and want to use it to handle my database connection for me. While we are at it, we are also adding Rake. ActiveRecord’s gem includes all the tasks you might be used to from Ruby on Rails, and with Rake you can use them in just the same way as in Ruby on Rails:
Now that we have the right tools in place, let’s look at the configuration.
Configuring the database
To make everything as comfortable as possible for you, we are going to follow
the conventions of Ruby on Rails and put the database configuration in
config/database.yml. With SQLite for the development and test
environment, and PostgreSQL for production, our configuration
file looks like this:
The production environment is configured via environment variables, what allows us to keep our sensitive passwords out of the code base and out of version control. In case the variables are not set, we fall back to default values that are also going to configure in Docker as the credentials for the database. This makes it easy to use the application with Docker for development purposes.
Configuring the application
The next step is to make ActiveRecord available to our application. While we have installed the gem and provided its configuration, we are not yet able to use it within our code.
There are two things we need to do:
- Add a
requirestatement to pull in ActiveRecord
- Tell it where to find its configuration
Look at the beginning of our
app.rb file to see how this is done:
See the next section for an example.
Let’s say we want to manage Resources. First, let’s create a class for them.
Put it in
app.rb, above the first
Now that we have our Resource class, we can implement the
for it. We start by extending the
Next up is the
These are very basic implementations, just to show you that everything works as expected. For an application that is aimed for production, I strongly advice you to do a more robust implementation than this.
Creating the table
Before we can do anything with this newly written code, we need to create the table for our Resource class. One of the nicest things about ActiveRecord is the support for migrations, which define changes you make to the database as code that can be run in a reproducible way. So let’s create a migration. Remember Rake?
The output of this command is the file path to your newly created migration. Open it, and add the following instructions:
Just like you would in Ruby on Rails, you can run this migration with the following command:
The migration created the table
resources for you, with an implicit field
id, the specified field
name and timestamps containing the times of creation
and the last update.
You can now start the application and browse to its URL. The output from the
#index method should be
, since we don’t have created any resources yet.
Setting up Sinatra with ActiveRecord is not
difficult. We installed the necessary dependencies via our
the database connection in
config/database.yml, created and ran a
migration with Rake and finally added a little bit of
configuration to our
app.rb to make it use ActiveRecord.
The provided code examples are very basic implementations of what is possible, and I strongly recommend that you build a more solid API if you want to try Sinatra. Two things that immediately come to mind when thinking about areas to improve are authentication and pagination. Right now, everyone can create and delete resources like he wants. That is almost never what you want, though.
You might want to check out the series as well, since it described how to pack everything we did today into a Docker container and make it portable. Start with the first post:
If you have any questions or suggestions for improvement, feel free to leave a comment below. I would love to hear from you.
Hope to see you in the next post!