Sinatra Todo app with Datamapper using Postgres and ERB

By | 20/09/2014

In my previous post, I have build a Todo app based on Sinatra with Datamapper and SQlite. For this post, I will be using again the Todo app, but I will use Postgres database instead. It’s amazingly simple to achieve this using Datamapper. The reason why this is important is because tools like Heroku do not support SQLite, but they do support Postgres.

As a nice add on, I will also create a better structure for my Sinatra apps in general. I don’t like the fact that everything is packed into a single app.rb file. Let’s get started!

Change to Postgress

configure :development do
   DataMapper.setup(
    :default,
    'postgres://user:user123@localhost/test'
  )
end

configure :production do
  DataMapper.setup(
    :default,
    'postgres://postgres:12345@localhost/sinatra_service'
  )
end

As a comparison, in my previous post using SQLite it used to be:

require "sinatra"
require "data_mapper"

DataMapper.setup(
    :default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/todo.db")

And that’s really all there is to changing from SQlite to Postgres.

Applying the new structure

So far, all are changes were done in a single app.rb file. While this is a perfectly good solution for Sinatra, it becomes very messy sometimes and easy to lose the overview. So let’s re-structure things a bit:

Sinatra_structure

You can see we created a folder ‘routes’, a folder ‘models’. The ‘routes’ folder will contain all the code related to the routing of our app while the ‘model’ folder will contain all our models in seperate files.

In each folder an init.rb file is also available. This contains links to the files in the directory the index file resides. So in other words, the init.rb file in the ‘model’ folder contains:

require_relative './todo'

Using the require_relative method, we are importing the todo.rb file in the ‘model’ folder.

The main.rb file is the file that keeps everything glued together.

# encoding: UTF-8
require 'json'
require 'sinatra'
require 'data_mapper'
require 'dm-migrations'
require 'sinatra/flash'

enable :sessions

configure :development do
   DataMapper.setup(
    :default,
    'postgres://user:user123@localhost/test'
  )
end

configure :production do
  DataMapper.setup(
    :default,
    'postgres://postgres:12345@localhost/sinatra_service'
  )
end

require './models/init'
require './helpers/init'
require './routes/init'

DataMapper.finalize 

This main file contains the section to setup datamapper and will also include the models filers, the helper files and the routes files. For each, it refers to the appropriate folder.

While this is merely some reshuffling of our code base, it really provides a better structure to keep the overview of your app. At least, all route files are grouped, all models are grouped etc…

In order to run this application, you will need to go to your console and do “rake migrate” (after you configured your postgres on your local server of course) and “shotgun”

The full source code can be found on Github