Presentation on replacing ActiveRecord with DataMapper in a Ruby on Rails application. Blog posting with info: http://degenportnoy.blogspot.com/2008/12/replacing-activerecord-with-datamapper.html
Source: http://github.com/PeterDP/ar2dm
6. DataMapper Goodness
●
Identity Map
●
Mappings in Model
●
Multiple Repositories
●
Lazy Loads of Large Lumps
●
Strategic Eager Loading
7. Identity Map
●
One object for one row
●
Reduces hits to database
●
Exists during Identity Map Session
8. Mapping in Model
●
Persist any class
●
Independent data store evolution
●
Connect to multiple databases
●
DM issues updates or creates only for what
it knows
9. Multiple Repositories
●
Nested YAML structure
●
Override target table names
●
Specify connection per class or even call
10. Lazy Loading
●
Reduces load of large columns
●
Text
●
Blobs
●
Graphics
●
Specify which columns lazy load
●
Lazy loads happen together; only 1 more call
●
Can be grouped
19. Demo Applications
●
One application to test ActiveRecord
●
One application to test DataMapper
●
Each create same data structure:
●
script/generate model book amount:float
date:date name:string description:text
--skip-timestamps
●
Create 1000 records
●
Profile & Benchmark
20. Gems You Need
●
data_mapper
●
do_mysql (or do_postgres or do_sqlite3)
●
dm-core
●
fastthread
●
json
●
rspec
21. Edit config/environment.rb
●
config.frameworks -= [ :active_record ]
●
config.gem “do_mysql”
●
config.gem “dm-core”
●
config.gem “dm-migrations”
Make sure your gems are up to date by
running “sudo rake gems:install”
24. Generate Model
> script/generate dm_model book amount:float date:date name:string
description:text --skip-migration
NOTE: --skip-migration is needed because the rails-datamapper integration
gem doesn't pull out the ActiveRecord dependency on migration creation
yet.
25. Finish model definition
require 'lorem'
class Book
include DataMapper::Resource
property :id, Integer, :serial => true
property :amount, Float
property :date, Date
property :name, String
property :description, Text, :lazy => true
def self.create_dummy_data(num)
num.times do |number|
t = Book.new
t.attributes = {:amount => number, :date => Time.now(),
:name => Lorem.lorem(4, false), :description => Lorem.lorem(60, false)}
t.save
end
end
end
26. Create Migration
migration 1, :create_books do
up do
create_table :books do
column :id, Integer, :serial => true, :nullable? => false, :key => true
column :amount, Float
column :date, Date
column :name, String
column :description, DataMapper::Types::Text
end
end
down do
drop_table :books
end
end
27. Create Migration Rake Task
namespace :dm do
task :migrate => :environment do
gem quot;dm-migrationsquot;
require quot;migration_runnerquot;
Dir[File.join(Rails.root, quot;dbquot;, quot;migratequot;, quot;*quot;)].each {|f| require f}
migrate_up!
end
end
src/dm2> mysqladmin create dm2_dev
src/dm2> rake dm:migrate
28. Wire up application
●
Create Controller for book
●
Create “bulk” generation capability