Replacing ActiveRecord With DataMapper

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    9 Favorites

    Replacing ActiveRecord With DataMapper - Presentation Transcript

    1. Replacing ActiveRecord with DataMapper in Ruby on Rails Peter Degen-Portnoy November 25, 2008
    2. DataMapper is an ORM
    3. A lot like ActiveRecord ● DB Adapters ● Migrations ● Associations ● One to one ● One to many ● Many to many ● Many to one
    4. Application Rails Merb ORM DataMapper Adaptors DO.rb YAML IMAP Storage
    5. Most often used with Merb ...but not exclusively
    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
    11. Strategic Eager Loading ● One call per child ● Eliminates n+1 calls for associations
    12. Way faster than ActiveRecord
    13. 10,000 records fetched w/ Active Record Average: 1.3429 seconds
    14. 10,000 records fetched w/ DataMapper Average: 0.0002 seconds
    15. 1.3429 seconds compared to 0.0002 seconds
    16. 0.0150% as much time
    17. To Fetch 1,000 Records ActiveRecord Over 204,000 calls DataMapper Under 7,000 calls
    18. Replacing ActiveRecord with DataMapper the part for which you have been waiting
    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”
    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. Create config/initializers/datamapper.rb require \"dm-core\" hash = YAML.load(File.new(Rails.root + \"/config/database.yml\")) DataMapper.setup(:default, 'mysql://root:mysql@localhost/dm2_dev' )
    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 \"dm-migrations\" require \"migration_runner\" Dir[File.join(Rails.root, \"db\", \"migrate\", \"*\")].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
    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. Additional Goodness ● Validations ● Conditions ● Order ● Aggregations ● Associations ● Named Scope (class methods)
    31. Assessment ● Can't beat the speed ● Outstanding flexibility ● Durn if it won't be more complex to develop with
    32. Thank you

    + Peter Degen-PortnoyPeter Degen-Portnoy, 11 months ago

    custom

    2748 views, 9 favs, 0 embeds more stats

    Presentation on replacing ActiveRecord with DataMap more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2748
      • 2748 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 9
    • Downloads 48
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories