MongoMapper          Kerry BuckleyIPRUG Lightning talk, 2 August 2011
What is it?
What is it?
Why we switched
Why we switched• Modelling complex object hierarchy• Painfully-complex polymorphic associations • (and about to get worse)...
Document   class User include MongoMapper::Document key :username, String key :real_name, String key :date_of_birth, Dateend
Supported types• Array      • Object    • Date• Float      • String    • ObjectId• Hash       • Time      • Set• Integer  ...
Custom types  module MongoMapper::BigDecimal def to_mongo value  value.to_s end def from_mongo value  value.present? ? new...
Database migrations
Database migrations  This page intentionally left blank
Time and user stamps   class Article include MongoMapper::Document key :title, String key :body, String timestamps! userst...
Simple associations   class Owner include MongoMapper::Document many :petsendclass Pet include MongoMapper::Document belon...
Embedded document   class Order include MongoMapper::Document many :line_itemsendclass LineItem include MongoMapper::Embed...
Many-to-many   class Book include MongoMapper::Document key :title key :author_ids, Array many :authors, :in => :author_id...
Basic polymorphism   class BlogPost include MongoMapper::Document key :body many :comments, :as => :commentableendclass Ho...
SCI polymorphismclass Commentable include MongoMapper::Document many :commentsendclass BlogPost < Commentable key :bodyend...
Reverse polymorphism   class Site include MongoMapper::Document many :pagesendclass Page include MongoMapper::Document bel...
M2M polymorphism   class Book include MongoMapper::Document key :author_ids, Array many :authors, :in => :author_idsendcla...
Validations   class Member include MongoMapper::Document key :name, String key :age, Integer key :sex, String validates_un...
Validation shorthand   class Member include MongoMapper::Document key :name, String, :required => true, :unique => true ke...
Querying   class User include MongoMapper::Document key :first_name, String key :last_name, String scope :smiths, where(:la...
Modifiers   class Page include MongoMapper::Document key :title,   String key :hit_count, Integer, :default => 0 key :tags...
Gotchas
Gotchas• No transactions• find versus find!• Writes are fire-and-forget by default• Uniqueness is per class, not per collecti...
Reflections
Reflections• Longish but fairly painless switch• Acceptance/integration tests invaluable• Specs/unit tests a pain to migra...
Fin.
Upcoming SlideShare
Loading in...5
×

MongoMapper lightning talk

1,223

Published on

Lightning talk from IPRUG, 2 August 2011.

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,223
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • MongoMapper lightning talk

    1. 1. MongoMapper Kerry BuckleyIPRUG Lightning talk, 2 August 2011
    2. 2. What is it?
    3. 3. What is it?
    4. 4. Why we switched
    5. 5. Why we switched• Modelling complex object hierarchy• Painfully-complex polymorphic associations • (and about to get worse)• Data seemed to suit a document DB
    6. 6. Document class User include MongoMapper::Document key :username, String key :real_name, String key :date_of_birth, Dateend
    7. 7. Supported types• Array • Object • Date• Float • String • ObjectId• Hash • Time • Set• Integer • Binary• NilClass • Boolean
    8. 8. Custom types module MongoMapper::BigDecimal def to_mongo value value.to_s end def from_mongo value value.present? ? new(value.to_s) : nil endendBigDecimal.send :extend, MongoMapper::BigDecimalclass Person include MongoMapper::Document key :height, BigDecimalend
    9. 9. Database migrations
    10. 10. Database migrations This page intentionally left blank
    11. 11. Time and user stamps class Article include MongoMapper::Document key :title, String key :body, String timestamps! userstamps!endclass User include MongoMapper::Document key :name, Stringend
    12. 12. Simple associations class Owner include MongoMapper::Document many :petsendclass Pet include MongoMapper::Document belongs_to :owner one :bedendclass Bed include MongoMapper::Document belongs_to :petend
    13. 13. Embedded document class Order include MongoMapper::Document many :line_itemsendclass LineItem include MongoMapper::EmbeddedDocument key :name, String key :quantity, Integerend
    14. 14. Many-to-many class Book include MongoMapper::Document key :title key :author_ids, Array many :authors, :in => :author_idsendclass Author include MongoMapper::Document key :nameend
    15. 15. Basic polymorphism class BlogPost include MongoMapper::Document key :body many :comments, :as => :commentableendclass HomePage include MongoMapper::Document key :content many :comments, :as => :commentableendclass Comment include MongoMapper::Document key :text belongs_to :commentable, :polymorphic => true
    16. 16. SCI polymorphismclass Commentable include MongoMapper::Document many :commentsendclass BlogPost < Commentable key :bodyendclass HomePage < Commentable key :contentendclass Comment include MongoMapper::Document key :text belongs_to :commentableend
    17. 17. Reverse polymorphism class Site include MongoMapper::Document many :pagesendclass Page include MongoMapper::Document belongs_to :siteendclass HomePage < Page key :contentendclass BlogPost < Page key :body
    18. 18. M2M polymorphism class Book include MongoMapper::Document key :author_ids, Array many :authors, :in => :author_idsendclass Novel < Bookendclass Textbook < Bookendclass Author include MongoMapper::Documentendclass Editor < Authorend
    19. 19. Validations class Member include MongoMapper::Document key :name, String key :age, Integer key :sex, String validates_uniqueness_of :name validates_presence_of :name validates_numericality_of :age validates_inclusion_of :sex, :in => %w{male female} validate :custom_validation def custom_validation ... endend
    20. 20. Validation shorthand class Member include MongoMapper::Document key :name, String, :required => true, :unique => true key :age, Integer, :numeric => true key :sex, String, :in => %w{male female} validate :custom_validation def custom_validation ... endend
    21. 21. Querying class User include MongoMapper::Document key :first_name, String key :last_name, String scope :smiths, where(:last_name => "Smith")endUser.find "4e0c70aea7a6c32ab5000003"User.find_by_first_name "Kerry"User.find_by_first_name! "Kerry"User.find_by_first_name_and_last_name "Kerry", "Buckley"User.find_all_by_last_name "Buckley"User.smiths.find_by_first_name "John"
    22. 22. Modifiers class Page include MongoMapper::Document key :title, String key :hit_count, Integer, :default => 0 key :tags, Arrayend@page.increment :hit_count => 1Page.increment {:title => "Home"}, :hit_count => 1Page.set {:title => "Home"}, :title => "New Home"Page.unset {:title => "Home"}, :titlePage.push {:title => "Home"}, :tags => "foo"
    23. 23. Gotchas
    24. 24. Gotchas• No transactions• find versus find!• Writes are fire-and-forget by default• Uniqueness is per class, not per collection• Formtastic needs hints of field type
    25. 25. Reflections
    26. 26. Reflections• Longish but fairly painless switch• Acceptance/integration tests invaluable• Specs/unit tests a pain to migrate• Decouple behaviour from persistence!
    27. 27. Fin.
    1. A particular slide catching your eye?

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

    ×