Skip to content
robholland edited this page Sep 13, 2010 · 20 revisions

This section covers the specifics of setting up an application using the sinatra_more generators and each database component.

DataMapper

To generate an application using datamapper as the ORM, invoke the sinatra_more generator:

$ sinatra_gen demo_app . -d datamapper

You also need to make sure you have the following gems installed:

$ sudo gem install data_objects
$ sudo gem install do_sqlite3      # for sqlite3
$ sudo gem install do_postgres  # for postgres
$ sudo gem install do_mysql      # for mysql

Next, we need to configure the database setup options. The connection line can be found in config/initializers/data_mapper.rb. In here, we can change the DataMapper.setup call to configure the database:

# config/initializers/data_mapper.rb
# ...
app.configure(:development) { 
    DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/blog.sqlite3") }
app.configure(:production) { 
    DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/blog_production.sqlite3") }
app.configure(:test) { 
    DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/blog_test.sqlite3") }

This should be all that needs to be done to configure datamapper in your application. Now you can add additional datamapper models into your app/models directory. The definition for a dm model looks like the following:

class Post
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  property :body, Text
  property :created_at, DateTime
end

For more information about DataMapper, check out the DataMapper Documentation

MongoMapper

To generate an application using mongo_mapper as the ORM, invoke the sinatra_more generator:

$ sinatra_gen demo_app . -d mongomapper

First, you need to install the native mongodb binaries from the mongodb website (or through MacPorts or a package management system).

In addition, you also need to make sure you have the following gems installed:

$ sudo gem install mongo --source http://gemcutter.org
$ sudo gem install mongo_mapper --source http://gemcutter.org

Be sure to start the mongodb database by using:

mongod --dbpath /path/to/db

Next, we need to configure the database setup options. The connection line can be found in config/initializers/mongo_db.rb. In here, we can change the MongoMapper.connection to configure the database:

# config/initializers/mongo_db.rb
app.configure :development do
  MongoMapper.connection = Mongo::Connection.new('localhost')
  MongoMapper.database = 'blog'
end
app.configure :production do
 MongoMapper.connection = Mongo::Connection.new('localhost')
 MongoMapper.database = 'blog_production'
end
app.configure :test do
  MongoMapper.connection = Mongo::Connection.new('localhost')
  MongoMapper.database = 'test_blog'
end

This should be all that needs to be done to configure mongo_mapper in your application. Now you can add additional mongo_mapper models into your app/models directory. The definition for a mongo model document looks like the following:

class Post
  include MongoMapper::Document
  key :title,        String
  key :body,       String
  key :created_at, Time
end

For more information about MongoMapper, check out the MongoMapper Readme

ActiveRecord

To generate an application using active_record as the ORM, invoke the sinatra_more generator:

$ sinatra_gen demo_app . -d activerecord

Next, we need to configure the database setup options. The connection line can be found in config/initializers/active_record.rb. In here, we can change the ActiveRecord::Base.establish_connection call to configure the database:

# config/initializers/active_record.rb
app.configure :development do
  ActiveRecord::Base.establish_connection(
       :adapter => 'sqlite3', :database => 'blog.db')
end
app.configure :production do
  ActiveRecord::Base.establish_connection(
       :adapter => 'sqlite3', :database => 'blog_production.rb')
end
app.configure :test do
  ActiveRecord::Base.establish_connection(
       :adapter => 'sqlite3', :database => 'blog_test.rb')
end

Now the database needs to be migrated to define the users table for development and test environments:

$ rake db:migrate
$ rake db:migrate RACK_ENV=test

This should be all that needs to be done to configure activerecord in your application. Now you can add additional activerecord models into your app/models directory. The definition for a ar model looks like the following:

class Post < ActiveRecord::Base
  # no schema, this is found in a migration
end

To create the migration, simply add a file to the db/migrate directory:

# db/migrate/002_create_posts.rb
class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
       t.column :title, :string
       t.column :body, :text
       t.column :created_at, :datetime
    end
  end
  def self.down
    drop_table :posts
  end
end

For more information about ActiveRecord, check out the ActiveRecord Documentation

Sequel

To generate an application using sequel as the ORM, invoke the sinatra_more generator:

$ sinatra_gen demo_app . -d sequel

Next, we need to configure the database setup options. The connection line can be found in config/initializers/sequel.rb. In here, we can change the Sequel.connect call to configure the database:

# config/initializers/sequel.rb
app.configure(:development) { Sequel.connect("sqlite://#{Dir.pwd}/blog.db") }
app.configure(:production) { Sequel.connect("sqlite://#{Dir.pwd}/blog_live.db") }
app.configure(:test)  { Sequel.connect"sqlite://#{Dir.pwd}/blog_test.db") }

This should be all that needs to be done to configure sequel in your application. Now you can add additional sequel models into your app/models directory. The definition for a sequel model looks like the following:

class Post < Sequel::Model(:posts)
  unless table_exists?
    set_schema do
      primary_key :id
      string :title
      string :body
      datetime :created_at
    end
    create_table
  end
end

For more information about Sequel, check out the Sequel Documentation

CouchREST

To generate an application using couchrest as the ORM, invoke the sinatra_more generator:

$ sinatra_gen demo_app . -d couchrest
Clone this wiki locally