Skip to content
nesquena 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 dm-core 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
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/blog.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
MongoMapper.connection = Mongo::Connection.new('localhost')
MongoMapper.database = 'blog'

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

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

$ sudo gem install 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
ActiveRecord::Base.establish_connection(
        :adapter => 'sqlite3',
        :database => 'blog.db'
)

Now the database needs to be migrated to define the users table:

$ rake db:migrate

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

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

$ sudo gem install 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
Sequel.connect("sqlite://#{Dir.pwd}/blog.sqlite3")

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 DB.table_exists?(:posts)
    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

Clone this wiki locally