SlideShare a Scribd company logo
1 of 27
MongoMapper
          Kerry Buckley
IPRUG 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)
• Data seemed to suit a document DB
Document

   class User
 include MongoMapper::Document

 key :username, String
 key :real_name, String
 key :date_of_birth, Date
end
Supported types
• Array      • Object    • Date

• Float      • String    • ObjectId

• Hash       • Time      • Set

• Integer    • Binary

• NilClass   • Boolean
Custom types
  module MongoMapper::BigDecimal
 def to_mongo value
  value.to_s
 end

 def from_mongo value
  value.present? ? new(value.to_s) : nil
 end
end
BigDecimal.send :extend, MongoMapper::BigDecimal

class Person
 include MongoMapper::Document
 key :height, BigDecimal
end
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!
 userstamps!
end

class User
 include MongoMapper::Document

 key :name, String
end
Simple associations
   class Owner
 include MongoMapper::Document
 many :pets
end

class Pet
 include MongoMapper::Document
 belongs_to :owner
 one :bed
end

class Bed
 include MongoMapper::Document
 belongs_to :pet
end
Embedded document
   class Order
 include MongoMapper::Document

 many :line_items
end

class LineItem
 include MongoMapper::EmbeddedDocument

 key :name, String
 key :quantity, Integer
end
Many-to-many
   class Book
 include MongoMapper::Document

 key :title
 key :author_ids, Array
 many :authors, :in => :author_ids
end

class Author
 include MongoMapper::Document

 key :name
end
Basic polymorphism
   class BlogPost
 include MongoMapper::Document
 key :body
 many :comments, :as => :commentable
end

class HomePage
 include MongoMapper::Document
 key :content
 many :comments, :as => :commentable
end

class Comment
 include MongoMapper::Document
 key :text
 belongs_to :commentable, :polymorphic => true
SCI polymorphism
class Commentable
 include MongoMapper::Document
 many :comments
end

class BlogPost < Commentable
 key :body
end

class HomePage < Commentable
 key :content
end

class Comment
 include MongoMapper::Document
 key :text
 belongs_to :commentable
end
Reverse polymorphism
   class Site
 include MongoMapper::Document
 many :pages
end

class Page
 include MongoMapper::Document
 belongs_to :site
end

class HomePage < Page
 key :content
end

class BlogPost < Page
 key :body
M2M polymorphism
   class Book
 include MongoMapper::Document
 key :author_ids, Array
 many :authors, :in => :author_ids
end

class Novel < Book
end

class Textbook < Book
end

class Author
 include MongoMapper::Document
end

class Editor < Author
end
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
  ...
 end
end
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
  ...
 end
end
Querying
   class User
 include MongoMapper::Document
 key :first_name, String
 key :last_name, String
 scope :smiths, where(:last_name => "Smith")
end

User.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"
Modifiers
   class Page
 include MongoMapper::Document

 key :title,   String
 key :hit_count, Integer, :default => 0
 key :tags,     Array
end

@page.increment :hit_count => 1
Page.increment {:title => "Home"}, :hit_count => 1

Page.set {:title => "Home"}, :title => "New Home"

Page.unset {:title => "Home"}, :title

Page.push {:title => "Home"}, :tags => "foo"
Gotchas
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
Reflections
Reflections

• Longish but fairly painless switch
• Acceptance/integration tests invaluable
• Specs/unit tests a pain to migrate
• Decouple behaviour from persistence!
Fin.

More Related Content

What's hot

Webinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real WorldWebinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real WorldMongoDB
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real WorldMike Friedman
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo dbMongoDB
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patternsjoergreichert
 
01 ElasticSearch : Getting Started
01 ElasticSearch : Getting Started01 ElasticSearch : Getting Started
01 ElasticSearch : Getting StartedOpenThink Labs
 
06. ElasticSearch : Mapping and Analysis
06. ElasticSearch : Mapping and Analysis06. ElasticSearch : Mapping and Analysis
06. ElasticSearch : Mapping and AnalysisOpenThink Labs
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and JavaBuilding a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Javaantoinegirbal
 
Data Modeling Deep Dive
Data Modeling Deep DiveData Modeling Deep Dive
Data Modeling Deep DiveMongoDB
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBMongoDB
 
MongoDB & Mongoid with Rails
MongoDB & Mongoid with RailsMongoDB & Mongoid with Rails
MongoDB & Mongoid with RailsJustin Smestad
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsMongoDB
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentationMurat Çakal
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?codeandyou forums
 

What's hot (20)

Webinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real WorldWebinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real World
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 
01 ElasticSearch : Getting Started
01 ElasticSearch : Getting Started01 ElasticSearch : Getting Started
01 ElasticSearch : Getting Started
 
06. ElasticSearch : Mapping and Analysis
06. ElasticSearch : Mapping and Analysis06. ElasticSearch : Mapping and Analysis
06. ElasticSearch : Mapping and Analysis
 
Json
JsonJson
Json
 
Python and MongoDB
Python and MongoDBPython and MongoDB
Python and MongoDB
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and JavaBuilding a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Java
 
Data Modeling Deep Dive
Data Modeling Deep DiveData Modeling Deep Dive
Data Modeling Deep Dive
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDB
 
MongoDB & Mongoid with Rails
MongoDB & Mongoid with RailsMongoDB & Mongoid with Rails
MongoDB & Mongoid with Rails
 
JSON
JSONJSON
JSON
 
Json
JsonJson
Json
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentation
 
Json
JsonJson
Json
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
 

Viewers also liked

Examples Of Good And Bad Essays
Examples Of Good And Bad EssaysExamples Of Good And Bad Essays
Examples Of Good And Bad Essaysmissm
 
Film Studies Research: A Quick Introduction to Library Resources
Film Studies Research: A Quick Introduction to Library ResourcesFilm Studies Research: A Quick Introduction to Library Resources
Film Studies Research: A Quick Introduction to Library ResourcesAlexander Affleck
 

Viewers also liked (8)

Intro To Library Research
Intro To Library ResearchIntro To Library Research
Intro To Library Research
 
Git
GitGit
Git
 
D.B.
D.B.D.B.
D.B.
 
A Murmuration Of Starlings
A Murmuration Of StarlingsA Murmuration Of Starlings
A Murmuration Of Starlings
 
Examples Of Good And Bad Essays
Examples Of Good And Bad EssaysExamples Of Good And Bad Essays
Examples Of Good And Bad Essays
 
The bad essay
The bad essayThe bad essay
The bad essay
 
Film Studies Research: A Quick Introduction to Library Resources
Film Studies Research: A Quick Introduction to Library ResourcesFilm Studies Research: A Quick Introduction to Library Resources
Film Studies Research: A Quick Introduction to Library Resources
 
Moodle Graz
Moodle GrazMoodle Graz
Moodle Graz
 

Similar to MongoMapper lightning talk

Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and HarmonySteve Smith
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)MongoSF
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperGiordano Scalzo
 
Simple MongoDB design for Rails apps
Simple MongoDB design for Rails appsSimple MongoDB design for Rails apps
Simple MongoDB design for Rails appsSérgio Santos
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Railsrfischer20
 
The Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystemThe Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystemHarold Giménez
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012Yaqi Zhao
 
NoSQL を Ruby で実践するための n 個の方法
NoSQL を Ruby で実践するための n 個の方法NoSQL を Ruby で実践するための n 個の方法
NoSQL を Ruby で実践するための n 個の方法Tomohiro Nishimura
 
MacAD.UK 2018: Your Code Should Document Itself
MacAD.UK 2018: Your Code Should Document ItselfMacAD.UK 2018: Your Code Should Document Itself
MacAD.UK 2018: Your Code Should Document ItselfBryson Tyrrell
 
Model Driven Software Development - Data Model Evolution
Model Driven Software Development - Data Model EvolutionModel Driven Software Development - Data Model Evolution
Model Driven Software Development - Data Model EvolutionSander Vermolen
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real worldKevin Faustino
 
Webinar: Building Your First App
Webinar: Building Your First AppWebinar: Building Your First App
Webinar: Building Your First AppMongoDB
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosMongoDB
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedMongoDB
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB AppHenrik Ingo
 

Similar to MongoMapper lightning talk (20)

Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and Harmony
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)
 
Mongodb mongoid
Mongodb mongoidMongodb mongoid
Mongodb mongoid
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
 
Simple MongoDB design for Rails apps
Simple MongoDB design for Rails appsSimple MongoDB design for Rails apps
Simple MongoDB design for Rails apps
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 
The Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystemThe Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystem
 
Elastic tire demo
Elastic tire demoElastic tire demo
Elastic tire demo
 
ActiveRecord vs Mongoid
ActiveRecord vs MongoidActiveRecord vs Mongoid
ActiveRecord vs Mongoid
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012
 
NoSQL を Ruby で実践するための n 個の方法
NoSQL を Ruby で実践するための n 個の方法NoSQL を Ruby で実践するための n 個の方法
NoSQL を Ruby で実践するための n 個の方法
 
MacAD.UK 2018: Your Code Should Document Itself
MacAD.UK 2018: Your Code Should Document ItselfMacAD.UK 2018: Your Code Should Document Itself
MacAD.UK 2018: Your Code Should Document Itself
 
Model Driven Software Development - Data Model Evolution
Model Driven Software Development - Data Model EvolutionModel Driven Software Development - Data Model Evolution
Model Driven Software Development - Data Model Evolution
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real world
 
Webinar: Building Your First App
Webinar: Building Your First AppWebinar: Building Your First App
Webinar: Building Your First App
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB App
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
 

More from Kerry Buckley

Testing http calls with Webmock and VCR
Testing http calls with Webmock and VCRTesting http calls with Webmock and VCR
Testing http calls with Webmock and VCRKerry Buckley
 
Ruby nooks & crannies
Ruby nooks & cranniesRuby nooks & crannies
Ruby nooks & cranniesKerry Buckley
 
Javasccript MV* frameworks
Javasccript MV* frameworksJavasccript MV* frameworks
Javasccript MV* frameworksKerry Buckley
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test communityKerry Buckley
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)Kerry Buckley
 
Adastral Park code retreat introduction
Adastral Park code retreat introductionAdastral Park code retreat introduction
Adastral Park code retreat introductionKerry Buckley
 
The secret life of bees
The secret life of beesThe secret life of bees
The secret life of beesKerry Buckley
 
Background processing
Background processingBackground processing
Background processingKerry Buckley
 
Katas, Contests and Coding Dojos
Katas, Contests and Coding DojosKatas, Contests and Coding Dojos
Katas, Contests and Coding DojosKerry Buckley
 
Kanban and Iterationless Working
Kanban and Iterationless WorkingKanban and Iterationless Working
Kanban and Iterationless WorkingKerry Buckley
 
Software Development Trends
Software Development TrendsSoftware Development Trends
Software Development TrendsKerry Buckley
 

More from Kerry Buckley (20)

Jasmine
JasmineJasmine
Jasmine
 
Testing http calls with Webmock and VCR
Testing http calls with Webmock and VCRTesting http calls with Webmock and VCR
Testing http calls with Webmock and VCR
 
BDD with cucumber
BDD with cucumberBDD with cucumber
BDD with cucumber
 
Ruby nooks & crannies
Ruby nooks & cranniesRuby nooks & crannies
Ruby nooks & crannies
 
TDD refresher
TDD refresherTDD refresher
TDD refresher
 
Javasccript MV* frameworks
Javasccript MV* frameworksJavasccript MV* frameworks
Javasccript MV* frameworks
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
 
Functional ruby
Functional rubyFunctional ruby
Functional ruby
 
Adastral Park code retreat introduction
Adastral Park code retreat introductionAdastral Park code retreat introduction
Adastral Park code retreat introduction
 
Ruby
RubyRuby
Ruby
 
Cloud
CloudCloud
Cloud
 
The secret life of bees
The secret life of beesThe secret life of bees
The secret life of bees
 
Background processing
Background processingBackground processing
Background processing
 
Katas, Contests and Coding Dojos
Katas, Contests and Coding DojosKatas, Contests and Coding Dojos
Katas, Contests and Coding Dojos
 
Rack
RackRack
Rack
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 
Kanban and Iterationless Working
Kanban and Iterationless WorkingKanban and Iterationless Working
Kanban and Iterationless Working
 
Software Development Trends
Software Development TrendsSoftware Development Trends
Software Development Trends
 

Recently uploaded

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

MongoMapper lightning talk

  • 1. MongoMapper Kerry Buckley IPRUG Lightning talk, 2 August 2011
  • 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. Document class User include MongoMapper::Document key :username, String key :real_name, String key :date_of_birth, Date end
  • 7. Supported types • Array • Object • Date • Float • String • ObjectId • Hash • Time • Set • Integer • Binary • NilClass • Boolean
  • 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 end end BigDecimal.send :extend, MongoMapper::BigDecimal class Person include MongoMapper::Document key :height, BigDecimal end
  • 10. Database migrations This page intentionally left blank
  • 11. Time and user stamps class Article include MongoMapper::Document key :title, String key :body, String timestamps! userstamps! end class User include MongoMapper::Document key :name, String end
  • 12. Simple associations class Owner include MongoMapper::Document many :pets end class Pet include MongoMapper::Document belongs_to :owner one :bed end class Bed include MongoMapper::Document belongs_to :pet end
  • 13. Embedded document class Order include MongoMapper::Document many :line_items end class LineItem include MongoMapper::EmbeddedDocument key :name, String key :quantity, Integer end
  • 14. Many-to-many class Book include MongoMapper::Document key :title key :author_ids, Array many :authors, :in => :author_ids end class Author include MongoMapper::Document key :name end
  • 15. Basic polymorphism class BlogPost include MongoMapper::Document key :body many :comments, :as => :commentable end class HomePage include MongoMapper::Document key :content many :comments, :as => :commentable end class Comment include MongoMapper::Document key :text belongs_to :commentable, :polymorphic => true
  • 16. SCI polymorphism class Commentable include MongoMapper::Document many :comments end class BlogPost < Commentable key :body end class HomePage < Commentable key :content end class Comment include MongoMapper::Document key :text belongs_to :commentable end
  • 17. Reverse polymorphism class Site include MongoMapper::Document many :pages end class Page include MongoMapper::Document belongs_to :site end class HomePage < Page key :content end class BlogPost < Page key :body
  • 18. M2M polymorphism class Book include MongoMapper::Document key :author_ids, Array many :authors, :in => :author_ids end class Novel < Book end class Textbook < Book end class Author include MongoMapper::Document end class Editor < Author end
  • 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 ... end end
  • 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 ... end end
  • 21. Querying class User include MongoMapper::Document key :first_name, String key :last_name, String scope :smiths, where(:last_name => "Smith") end User.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. Modifiers class Page include MongoMapper::Document key :title, String key :hit_count, Integer, :default => 0 key :tags, Array end @page.increment :hit_count => 1 Page.increment {:title => "Home"}, :hit_count => 1 Page.set {:title => "Home"}, :title => "New Home" Page.unset {:title => "Home"}, :title Page.push {:title => "Home"}, :tags => "foo"
  • 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
  • 26. Reflections • Longish but fairly painless switch • Acceptance/integration tests invaluable • Specs/unit tests a pain to migrate • Decouple behaviour from persistence!
  • 27. Fin.

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n