tame accidental complexity with ruby and mongomapper

61
Tame accidental complexity Introduction to NoSQL with MongoMapper Giordano Scalzo

Post on 19-Oct-2014

3.212 views

Category:

Documents


0 download

DESCRIPTION

Gentle introduction to MongoDb with Ruby and MongoMapper

TRANSCRIPT

Page 1: Tame Accidental Complexity with Ruby and MongoMapper

Tame accidental complexityIntroduction to NoSQL with MongoMapper

Giordano Scalzo

Page 2: Tame Accidental Complexity with Ruby and MongoMapper
Page 3: Tame Accidental Complexity with Ruby and MongoMapper

I’m not here to talk about performance

Page 4: Tame Accidental Complexity with Ruby and MongoMapper

I’m not here to talk about scalability

Page 5: Tame Accidental Complexity with Ruby and MongoMapper

but I’m here to talk about simplicity

Page 6: Tame Accidental Complexity with Ruby and MongoMapper

Rails has been a first step

Page 7: Tame Accidental Complexity with Ruby and MongoMapper

Anatomy of a Rails Application

Page 8: Tame Accidental Complexity with Ruby and MongoMapper

Anatomy of a Rails Application

view

Page 9: Tame Accidental Complexity with Ruby and MongoMapper

Anatomy of a Rails Application

view controller

Page 10: Tame Accidental Complexity with Ruby and MongoMapper

Anatomy of a Rails Application

modelview controller

Page 11: Tame Accidental Complexity with Ruby and MongoMapper

Different languages

modelview controller

html+css

Page 12: Tame Accidental Complexity with Ruby and MongoMapper

Different languages

modelview controller

html+css oop

Page 13: Tame Accidental Complexity with Ruby and MongoMapper

Different languages

modelview controller

html+css oop sql

Page 14: Tame Accidental Complexity with Ruby and MongoMapper

Impedance mismatch

modelview controller

html+css oop sql

Page 17: Tame Accidental Complexity with Ruby and MongoMapper

We need persistent objects!

Page 18: Tame Accidental Complexity with Ruby and MongoMapper

We need persistent objects!

class User def initialize(username, password) @username = username @password = password endend

Page 19: Tame Accidental Complexity with Ruby and MongoMapper

We need persistent objects!

{ username: "giordano", password: "123"}

Page 20: Tame Accidental Complexity with Ruby and MongoMapper

ActiveRecord tries its best

Page 21: Tame Accidental Complexity with Ruby and MongoMapper

We need something different

Page 22: Tame Accidental Complexity with Ruby and MongoMapper

Persistence

Page 23: Tame Accidental Complexity with Ruby and MongoMapper

Persistence

class User include MongoMapper::Documentend

Page 24: Tame Accidental Complexity with Ruby and MongoMapper

Persistence

class User include MongoMapper::Documentend

user = User.create({ :username => "giordano", :password => "123"})user.save

Page 25: Tame Accidental Complexity with Ruby and MongoMapper

Persistence

class User include MongoMapper::Documentend

user = User.create({ :username => "giordano", :password => "123"})user.save

puts User.all.last.to_mongo

Page 26: Tame Accidental Complexity with Ruby and MongoMapper

Persistence

{ "_id"=>BSON::ObjectId('4d643a274d8ff683dd000001'), "username"=>"giordano", "password"=>"123"}

Page 27: Tame Accidental Complexity with Ruby and MongoMapper

Types

Page 28: Tame Accidental Complexity with Ruby and MongoMapper

Types

class User include MongoMapper::Document key :username, String key :password , Stringend

Page 29: Tame Accidental Complexity with Ruby and MongoMapper

Built-in Types

Array, Binary, Boolean, Date, Float, Hash, Integer, Nil, ObjectId, Set, String, Time

Page 30: Tame Accidental Complexity with Ruby and MongoMapper

Custom Types

class DowncasedString def self.to_mongo(value) value.nil? ? nil : value.to_s.downcase end def self.from_mongo(value) value.nil? ? nil : value.to_s.downcase endend

Page 31: Tame Accidental Complexity with Ruby and MongoMapper

Custom Types

class User include MongoMapper::Document key :username, String key :password , String key :email, DowncasedStringend

Page 32: Tame Accidental Complexity with Ruby and MongoMapper

Custom Types

user = User.newuser.username = "giordano"user.password = "123"user.email = "[email protected]"

user.save

puts User.all.last.to_mongo

Page 33: Tame Accidental Complexity with Ruby and MongoMapper

Custom Types

{ "_id"=>BSON::ObjectId('4d6442d94d8ff684d3000001'), "username"=>"giordano", "password"=>"123", "email"=>"[email protected]"}

Page 34: Tame Accidental Complexity with Ruby and MongoMapper

Embedded Documents

Page 35: Tame Accidental Complexity with Ruby and MongoMapper

Embedded Documents

class Task include MongoMapper::EmbeddedDocument key :description, String key :pomodori, Integer key :is_done, Booleanend

Page 36: Tame Accidental Complexity with Ruby and MongoMapper

Embedded Documents

class User include MongoMapper::Document key :username, String key :password , String key :email, DowncasedString many :tasksend

Page 37: Tame Accidental Complexity with Ruby and MongoMapper

Embedded Documents

user.tasks << Task.new({ description: 'refactor server', pomodori: 8, is_done: false})

user.tasks << Task.new({ description: 'timer sound', pomodori: 2, is_done: false})

Page 38: Tame Accidental Complexity with Ruby and MongoMapper

Embedded Documents

{"_id"=>BSON::ObjectId('4d6575e84d8ff692e6000001'), "username"=>"giordano", "password"=>"123", "email"=>"[email protected]", "tasks"=>[{ "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000002'), "description"=>"refactor server", "pomodori"=>8, "is_done"=>false }, { "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000003'), "description"=>"timer sound", "pomodori"=>2, "is_done"=>false }]}

Page 39: Tame Accidental Complexity with Ruby and MongoMapper

Embedded Documents

{"_id"=>BSON::ObjectId('4d6575e84d8ff692e6000001'), "username"=>"giordano", "password"=>"123", "email"=>"[email protected]", "tasks"=>[{ "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000002'), "description"=>"refactor server", "pomodori"=>8, "is_done"=>false }, { "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000003'), "description"=>"timer sound", "pomodori"=>2, "is_done"=>false }]}

Page 40: Tame Accidental Complexity with Ruby and MongoMapper

Documents

class Task include MongoMapper::Document key :description, String key :pomodori, Integer key :is_done, Booleanend

Page 41: Tame Accidental Complexity with Ruby and MongoMapper

Documents

p User.all.last.to_mongo

{ "_id"=>BSON::ObjectId('4d657e924d8ff6949c000001'), "username"=>"giordano", "password"=>"123", "email"=>"[email protected]"}

Page 42: Tame Accidental Complexity with Ruby and MongoMapper

Documents

p User.all.last.tasks

[#<Task _id: BSON::ObjectId('4d65822b4d8ff69542000002'), description: "refactor server", pomodori: 8, is_done: false, user_id: BSON::ObjectId('4d65822b4d8ff69542000001') >, #<Task _id: BSON::ObjectId('4d65822b4d8ff69542000003'), description: "timer sound", pomodori: 2, is_done: false, user_id: BSON::ObjectId('4d65822b4d8ff69542000001') >]

Page 43: Tame Accidental Complexity with Ruby and MongoMapper

Validations & Callbacks

Page 44: Tame Accidental Complexity with Ruby and MongoMapper

Validations & Callbacks

class User include MongoMapper::Document key :username, String, validates_presence_of :username key :password, String validates_presence_of :passwordend

Page 45: Tame Accidental Complexity with Ruby and MongoMapper

Validations & Callbacks

class User include MongoMapper::Document key :username, String, :required => true key :password, String, :required => trueend

Page 46: Tame Accidental Complexity with Ruby and MongoMapper

Validations & Callbacks

validates_presence_ofvalidates_length_ofvalidates_format_ofvalidates_numericality_ofvalidates_acceptance_ofvalidates_confirmation_ofvalidates_inclusion_ofvalidates_exclusion_of

Page 47: Tame Accidental Complexity with Ruby and MongoMapper

Validations & Callbacks

before_save after_save before_create after_create before_update after_update before_validation after_validation before_validation_on_create after_validation_on_create before_validation_on_update after_validation_on_update before_destroy after_destroy validate_on_create validate_on_update validate

Page 48: Tame Accidental Complexity with Ruby and MongoMapper

Validations & Callbacks

forked in current gem 0.8.6using Rails3 ActiveModel in Rails3 branch just merged

Page 49: Tame Accidental Complexity with Ruby and MongoMapper

What about querying?

Page 50: Tame Accidental Complexity with Ruby and MongoMapper

What about querying?

Plucky: ActiveRecord-like language

query = User.where(:last_name.exists => true, :created_at.gte => from_date, :created_at.lt => Time.now) .skip(0).limit(5)

query.all

Page 51: Tame Accidental Complexity with Ruby and MongoMapper

What about querying?

Plucky: ActiveRecord-like language

query = User.where(:last_name.exists => true, :created_at.gte => from_date, :created_at.lt => Time.now) .skip(0).limit(5)

#<Plucky::Query created_at: { "$gte"=>"1", "$lt"=>2011-02-24 10:54:36 UTC}, last_name: {"$exists"=>true}, limit: 5, skip: 0>

Page 52: Tame Accidental Complexity with Ruby and MongoMapper

What about querying?

Plucky: ActiveRecord-like language

query = User.where(:last_name.exists => true) .where(:created_at.gte => from_date) .where(:created_at.lt => Time.now) .skip(0).limit(5)

#<Plucky::Query created_at: { "$gte"=>"1", "$lt"=>2011-02-24 10:54:36 UTC}, last_name: {"$exists"=>true}, limit: 5, skip: 0>

Page 53: Tame Accidental Complexity with Ruby and MongoMapper

What about plugins?

Page 54: Tame Accidental Complexity with Ruby and MongoMapper

What about plugins?AccessibleAssociationsCachingCallbacksCloneDirtyDocumentDynamic QueryingEmbeddedDocumentEqualityIdentityMapIndexesInspectKeysLogger

ModifiersPaginationPersistenceProtectedQueryingRailsSafeSingle Collection InheritanceScopesSerializationTimestampsUserstampsValidations

Page 55: Tame Accidental Complexity with Ruby and MongoMapper

Itʼs just a beginning

Page 57: Tame Accidental Complexity with Ruby and MongoMapper

http://mongoid.org/

Page 58: Tame Accidental Complexity with Ruby and MongoMapper
Page 59: Tame Accidental Complexity with Ruby and MongoMapper
Page 60: Tame Accidental Complexity with Ruby and MongoMapper
Page 61: Tame Accidental Complexity with Ruby and MongoMapper

@giordanoscalzo

www.slideshare.net/giordano

github.com/gscalzo

[email protected]