DATAMAPPER
contagion
1
Datamapper is not only an
ORM
Object-Relational Mapping
2
Mapping
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
property :created_at, DateTime
has n, :comments
end
3
Not that different?
DB adaptors
STI
Association Migration
Hooks
Validation
4
Different!
Faster
Easier
Flexible
5
Identity Map
@p = Post.first
@p.comments.each do |c|
c.post.object_id == @p.object_id
end
6
Loaded Set
>> @p = Post.all
=> [#<Post id=1>, #<Post id=2>]
>> @p.class
=> DataMapper::Collection
>> @p[0].colleaction
=> [#<Post id=1>, #<Post id=2>]
7
Strategy Eager Loading
@posts = Post.all
@posts.each do |p|
puts p.user.name
end
2 Queries
no more n+1 or :include
8
Lazy Loading
@posts = Post.all
#select id,title,created_at from posts;
@posts.each do |p|
puts p.body
#select body from posts where ....;
end
Only load what you need
( :fetch in 1.0? )
9
Lazy
p = Post.all
# no query happened!
p[0]
#select * from posts limit 1
Wait for the kicker methods:
each, map, count, etc.
10
Scoped
review =
Post.all(:title.like =>‘review’ )
my_review =
yes_p.all(:user_id =>my_id)
conditions are joined together
when the kicker kicks in
11
Query Path
Post.all('comments.email.like' => 'contagion%' )
# SELECT "posts"."id", "posts"."created_at" FROM
"posts" INNER JOIN "comments" ON ("posts"."id" =
"comments"."post_id") WHERE ("comments"."email" LIKE
'contagion%') ORDER BY "posts"."id"
12
Go off the Golden Path
repository(:default).adapter.resource_naming_convention =
DataMapper::NamingConventions::Resource::Underscored
class Stock
property :target_symbol, String, :key => true
property :target_type_id, String, :key => true
repository(:legacy) do
property :target_name, String
end
end
Custom Naming Convention
Composite Keys
Multiple databases
13
Datamapper is not only an
ORM
Object-Relational Mapping
14
Data
DatabaseDatabaseDatabases
DatabaseDatabaseFiles
DatabaseDatabaseWeb Services
15
Merb Rails
Your
App
DataMapper
DO
DB
Application
Adapters
Data Sources
Architecture
REST
Web
16
• 50 lines of code (read only)
• 1 hour (because I am stupid..)
• unified interface
• works with association !!
My Stupid MemCache
Adaptor
17
Modular
• rubygems
• dataobject
• dm-core
• dm-more
18
Custom Type
• Serial : Integer with auto increment
• CSV : Serialize array into string
• YAML、JSON
• Custom :
• implements load / dump / typecast
19
AutoValidation
property :email,
:nullable => false,
:length => (1..255),
:format => :email_address
20

Datamapper