0
Replacing ActiveRecord
   with DataMapper
    in Ruby on Rails



         Peter Degen-Portnoy
         November 25, 2008
DataMapper is an ORM
A lot like ActiveRecord
●
    DB Adapters
●
    Migrations
●
    Associations
    ●
        One to one
    ●
        One t...
Application       Rails                Merb




ORM                       DataMapper




Adaptors      DO.rb         YAML ...
Most often used with Merb

               ...but not exclusively
DataMapper Goodness
●
    Identity Map
●
    Mappings in Model
●
    Multiple Repositories
●
    Lazy Loads of Large Lumps...
Identity Map
●
    One object for one row
●
    Reduces hits to database
●
    Exists during Identity Map Session
Mapping in Model
●
    Persist any class
●
    Independent data store evolution
●
    Connect to multiple databases
●
    ...
Multiple Repositories
●
    Nested YAML structure
●
    Override target table names
●
    Specify connection per class or ...
Lazy Loading
●
    Reduces load of large columns
    ●
        Text
    ●
        Blobs
    ●
        Graphics
●
    Speci...
Strategic Eager Loading
●
    One call per child
●
    Eliminates n+1 calls for associations
Way faster than ActiveRecord
10,000 records fetched w/ Active Record
        Average: 1.3429 seconds
10,000 records fetched w/ DataMapper
       Average: 0.0002 seconds
1.3429 seconds compared to 0.0002 seconds
0.0150% as much time
To Fetch 1,000 Records

ActiveRecord           Over 204,000 calls

DataMapper             Under 7,000 calls
Replacing ActiveRecord with DataMapper

            the part for which you have been waiting
Demo Applications
●
    One application to test ActiveRecord
●
    One application to test DataMapper
●
    Each create sa...
Gems You Need
●
    data_mapper
●
    do_mysql (or do_postgres or do_sqlite3)
●
    dm-core
●
    fastthread
●
    json
●
...
Edit config/environment.rb
●
    config.frameworks -= [ :active_record ]
●
    config.gem “do_mysql”
●
    config.gem “dm-...
Configure database.yml
development: &defaults
 :adapter: mysql
 :database: dm2_dev
 :user: root
 :password: mysql
 :socket...
Create
 config/initializers/datamapper.rb
require quot;dm-corequot;
hash = YAML.load(File.new(Rails.root + quot;/config/da...
Generate Model
> script/generate dm_model book amount:float date:date name:string
description:text --skip-migration



NOT...
Finish model definition
require 'lorem'

class Book
 include DataMapper::Resource
 property :id, Integer, :serial => true
...
Create Migration
migration 1, :create_books do
 up do
   create_table :books do
    column :id, Integer, :serial => true, ...
Create Migration Rake Task
namespace :dm do
  task :migrate => :environment do
   gem quot;dm-migrationsquot;
   require q...
Wire up application
●
    Create Controller for book
●
    Create “bulk” generation capability
Summary Checklist
●
    Add gems & generate application
●
    Remove ActiveRecord & add DataMapper
●
    Configure databas...
Additional Goodness
●
    Validations
●
    Conditions
●
    Order
●
    Aggregations
●
    Associations
●
    Named Scope...
Assessment
●
    Can't beat the speed
●
    Outstanding flexibility
●
    Durn if it won't be more complex to develop
    ...
Thank you
Upcoming SlideShare
Loading in...5
×

Replacing ActiveRecord With DataMapper

11,098

Published on

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

Published in: Self Improvement, Technology
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
11,098
On Slideshare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
128
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Transcript of "Replacing ActiveRecord With DataMapper"

  1. 1. Replacing ActiveRecord with DataMapper in Ruby on Rails Peter Degen-Portnoy November 25, 2008
  2. 2. DataMapper is an ORM
  3. 3. A lot like ActiveRecord ● DB Adapters ● Migrations ● Associations ● One to one ● One to many ● Many to many ● Many to one
  4. 4. Application Rails Merb ORM DataMapper Adaptors DO.rb YAML IMAP Storage
  5. 5. Most often used with Merb ...but not exclusively
  6. 6. DataMapper Goodness ● Identity Map ● Mappings in Model ● Multiple Repositories ● Lazy Loads of Large Lumps ● Strategic Eager Loading
  7. 7. Identity Map ● One object for one row ● Reduces hits to database ● Exists during Identity Map Session
  8. 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. 9. Multiple Repositories ● Nested YAML structure ● Override target table names ● Specify connection per class or even call
  10. 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
  11. 11. Strategic Eager Loading ● One call per child ● Eliminates n+1 calls for associations
  12. 12. Way faster than ActiveRecord
  13. 13. 10,000 records fetched w/ Active Record Average: 1.3429 seconds
  14. 14. 10,000 records fetched w/ DataMapper Average: 0.0002 seconds
  15. 15. 1.3429 seconds compared to 0.0002 seconds
  16. 16. 0.0150% as much time
  17. 17. To Fetch 1,000 Records ActiveRecord Over 204,000 calls DataMapper Under 7,000 calls
  18. 18. Replacing ActiveRecord with DataMapper the part for which you have been waiting
  19. 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. 20. Gems You Need ● data_mapper ● do_mysql (or do_postgres or do_sqlite3) ● dm-core ● fastthread ● json ● rspec
  21. 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”
  22. 22. Configure database.yml development: &defaults :adapter: mysql :database: dm2_dev :user: root :password: mysql :socket: /var/run/mysqld/mysqld.sock :host: localhost test: <<: *default :database: dm2_test production: <<: *default :database: dm2_prod
  23. 23. Create config/initializers/datamapper.rb require quot;dm-corequot; hash = YAML.load(File.new(Rails.root + quot;/config/database.ymlquot;)) DataMapper.setup(:default, 'mysql://root:mysql@localhost/dm2_dev' )
  24. 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. 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. 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. 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. 28. Wire up application ● Create Controller for book ● Create “bulk” generation capability
  29. 29. Summary Checklist ● Add gems & generate application ● Remove ActiveRecord & add DataMapper ● Configure database.yml & datamapper.rb ● Create your database ● Generate your model, complete definition & migrate ● Continue defining application
  30. 30. Additional Goodness ● Validations ● Conditions ● Order ● Aggregations ● Associations ● Named Scope (class methods)
  31. 31. Assessment ● Can't beat the speed ● Outstanding flexibility ● Durn if it won't be more complex to develop with
  32. 32. Thank you
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×