1. DataMapper, l’ORM dans Rails 3 ?
DataMapper, l’ORM dans Rails 3 ?
Cyril Mougel
07 Mars 2009
2. DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
DataMapper c’est quoi ?
1
Les base de DataMapper
2
Les plugins DataMapper
3
3. DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
DataMapper c’est quoi ?
Commenc´ en F´vrier 2007
e e
Objet Relationel Model (Model Object Relationnel)
Support´ uniquement par la communaut´
e e
4. 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
7. DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Lazy Loading
1 c l a s s Animal
2 include DataMapper : : Resource
3 property : id , Serial
4 property : name , String #: l a z y => f a l s e
5 property : notes , Text #: l a z y => t r u e
6 end
7
8 animals = Animal . all # No r e q u e s t
animals . each do | pet |
9 # SELECT i d , name FROM A n i m a l s
10 pet . notes # SELECT n o t e s FROM A n i m a l s WHERE i d = x
11 pet . name # No more request
12 end
8. DataMapper, l’ORM dans Rails 3 ?
DataMapper c’est quoi ?
Eager Loading
1 zoos = Zoo . all
zoos . each do | zoo |
2
3 # SELECT ’ i d ’ , ’ name ’ FROM Zoos
4 # 1 ,3 ,5 ,6 , etc . . .
5
zoo . exhibits . each do | exhibit |
6
7 # F a i t un r e q u e t e p a r 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 w h e r e z o o i d IN ( 1 , 3 , 5 , 6 ,
etc . . . )
puts ” Zoo : #{zoo . name } , E x h i b i t : #{ e x h i b i t . name} ”
9
10 end
11 end
9. DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
DataMapper c’est quoi ?
1
Les base de DataMapper
2
Les plugins DataMapper
3
10. DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
Un classe Model
H´rite seulement de Object
e
Include de include DataMapper::Resource
Les propri´t´s d´clar´ dans les classes mod`les
ee e e e
11. DataMapper, l’ORM dans Rails 3 ?
Les base de DataMapper
1 class User
2 include DataMapper :: Resource
3
4 property : id , Serial
5 property : name , String
6 property : age , Integer
7
8 has :n , : projects
9 belongs_to : group
10 end
12. 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 ])
13. DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
DataMapper c’est quoi ?
1
Les base de DataMapper
2
Les plugins DataMapper
3
14. DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
dm-validations
validates xxx
validation dans les properties directement
1 require ’dm−v a l i d a t i o n s ’
2
3 c l a s s Account
4 include DataMapper : : Resource
5
6 : name ,
property String
7 : content , Text , : length => ( 1 0 0 . . 5 0 0 )
property
8 : email , String , : nullable => f a l s e , : unique =>
property
true , : format => : email_address ,
: messages => {
9
10 : presence => ” n e e d e m a i l ” ,
11 : is_unique => ” a l r e a d y u s e d ” ,
12 : format => ” n o t a e m a i l ”
}
13
14
15 v a l i da t e s _ l e n gt h : name , : max => 20
16
17 end
15. DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
dm-migrations
1 migration 1 , : create_orders do
2 up do
3 create_table ( : orders ) do
4 column ( : id , Integer , : serial => t r u e )
5 column ( : amount , Integer )
6 column ( : completed , Boolean )
7 column ( : description , String , : size => 2 5 5 )
8 column ( : created_at , DateTime )
9 column ( : updated_at , DateTime )
10 end
11 end
12
13 down do
14 drop_table ( : orders )
15 end
16 end
16. 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..
17. DataMapper, l’ORM dans Rails 3 ?
Les plugins DataMapper
Les ressources ?
http ://datamapper.org
http ://github.com/datamapper
http ://groups.google.com/group/datamapper