datamapper l orm dans rails 3
Embed Size (px)
DESCRIPTION
une présentation de DataMapperTRANSCRIPT

DataMapper, l’ORM dans Rails 3 ?
DataMapper, l’ORM dans Rails 3 ?
Cyril Mougel
07 Mars 2009

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
1 DataMapper c’est quoi ?
2 Les base de DataMapper
3 Les plugins DataMapper

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
DataMapper c’est quoi ?
Commence en Fevrier 2007
Objet Relationel Model (Model Object Relationnel)
Supporte uniquement par la communaute

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Pourquoi DataMapper ?
L’identity Map
La gestion des multi-repository natif
Lazy loading
Eager loading
Tout est ruby
Modulaire

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Identity Map
User.first == User.first
User.first == User.get(1)

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Gestion des multi-repository natif
User.all(:repository => :other)
User.all #:repository => :default

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Lazy Loading
1 c l a s s Animal
2 include DataMapper : : Resource3 property : id , Serial
4 property : name , String #: l a z y => f a l s e5 property : notes , Text #: l a z y => t r u e6 end78 animals = Animal . all # No r e qu e s t9 animals . each do | pet | # SELECT id , name FROM Animals
10 pet . notes # SELECT note s FROM Animals WHERE i d = x11 pet . name # No more r e q u e s t12 end

DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Eager Loading
1 zoos = Zoo . all2 zoos . each do | zoo |3 # SELECT ’ i d ’ , ’ name ’ FROM Zoos4 # 1 ,3 ,5 , 6 , e t c . . .56 zoo . exhibits . each do | exhibit |7 # Fa i t un r equ e t e par i t e r a t i o n :8 # S e l e c t ’ name ’ from e x h i b i t s where z o o i d IN (1 , 3 , 5 , 6 ,
e t c . . . )9 puts ”Zoo : #{zoo . name} , E x h i b i t : #{ e x h i b i t . name}”
10 end11 end

DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
1 DataMapper c’est quoi ?
2 Les base de DataMapper
3 Les plugins DataMapper

DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
Un classe Model
Herite seulement de Object
Include de include DataMapper::Resource
Les proprietes declare dans les classes modeles

DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
1 class User2 include DataMapper :: Resource34 property :id, Serial5 property :name , String6 property :age , Integer78 has :n, :projects9 belongs_to :group
10 end

DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
La recherche avec DataMapper
1 Zoo.first(:name.eql => ’Galveston ’)2 Person.all(:age.gt => 30)3 Person.all(:age.gte => 30)4 Person.all(:name.not => ’bob’)5 Person.all(:name.like => ’S%’, :id => [1, 2, 3,
4, 5])6 Person.all(:name.not => [’bob’,’rick’,’steve’])7 Person.all(: order => [:age.desc])

DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
1 DataMapper c’est quoi ?
2 Les base de DataMapper
3 Les plugins DataMapper

DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
dm-validations
validates xxxvalidation dans les properties directement
1 require ’dm−v a l i d a t i o n s ’23 c l a s s Account
4 include DataMapper : : Resource56 property : name , String
7 property : content , Text , : length => ( 1 0 0 . . 5 0 0 )8 property : email , String , : nullable => f a l s e , : unique =>
t rue , : format => : email_address ,9 : messages => {
10 : presence => ”need ema i l ” ,11 : is_unique => ” a l r e a d y used ” ,12 : format => ” not a ema i l ”13 }1415 validates_length : name , : max => 201617 end

DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
dm-migrations
1 migration 1 , : create_orders do2 up do3 create_table ( : orders ) do4 column ( : id , Integer , : serial => t r u e )5 column ( : amount , Integer )6 column ( : completed , Boolean )7 column ( : description , String , : size => 255)8 column ( : created_at , DateTime )9 column ( : updated_at , DateTime )
10 end11 end1213 down do14 drop_table ( : orders )15 end16 end

DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
plein d’autre plugins
dm-types
dm-timestamp
dm-tags
dm-sweatshop
dm-constraints
dm-is-list
etc..

DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
Les ressources ?
http ://datamapper.org
http ://github.com/datamapper
http ://groups.google.com/group/datamapper

DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
questions ?