DataMapper In 20 min

Post on 01-Sep-2014

23.533 views 3 download

Tags:

description

Overview of DataMapper and the differences between DM and other Ruby ORMs. Presentation given by Matt Aimonetti during MerbCamp 2008 in San Diego, CA.

Transcript of DataMapper In 20 min

DataMapper

i n 2 0 m i n

MeMatt

Aimonetti

irc: m a t t e t t i

MeC o n s u l t a n tbased in San Diego, CA

Blogs:http://merbist.com

http://railsontherun.com

DataMapper i n 2 0 m i n

Object Relational Mapper

DM=

DataMapper i n 2 0 m i n

Databases

Object OrientedLanguages

scalar values

objects

ORM

data

DataMapper i n 2 0 m i n

SELECT * FROM BookWHERE price > 100.00ORDER BY title;

Raw SQL statements FTL

DataMapper i n 2 0 m i n

Book.all( :price.gt => 100.00, :order => [:title.asc] )

Pure Ruby FTW

DataMapper i n 2 0 m i n

Book.find( :all, :conditions => ["price > ?", 100.00],:order => "title" )

¿SQL/Ruby FAIL?

DataMapper i n 2 0 m i n

DM is MORE THAN THAT

DataMapper i n 2 0 m i n

@parent = Parent.find_by_name("Bob")

@parent.children.each do |child| @parent.object_id.should == child.parent.object_idend

identity map

FAILS with ActiveRecord

PASSES with DataMapper

DataMapper i n 2 0 m i n

Does what ActiveRecord doesbut differently

DataMapper i n 2 0 m i n

Data Objects

db drivers using 1 unified interface

Cop

yrig

ht -

Mer

b in

Act

ion

- M

anni

ng

DataMapper i n 2 0 m i n

Lazy loading+

Strategic Eager Loader

Procrastinationbecomes anORM value

DataMapper i n 2 0 m i n Lazy loading + SEL

#first, #each, #count, #map...DM only generates a query when encountering a kicker

DataMapper i n 2 0 m i n

zoos = Zoo.allzoos.each do |zoo| zoo.exhibits.map{|e| e.name}end

Lazy loading + SEL

DataMapper i n 2 0 m i n

SELECT * FROM "zoos" SELECT * FROM "exhibits" WHERE ("exhibits".zoo_id = 1) SELECT * FROM "exhibits" WHERE ("exhibits".zoo_id = 2) SELECT * FROM "exhibits" WHERE ("exhibits".zoo_id = 3)

ActiveRecord4 requests

Lazy loading + SEL

DataMapper i n 2 0 m i n

SELECT "id", "name" FROM "zoos" ORDER BY "id"SELECT "id", "name", "zoo_id" FROM "exhibits" WHERE ("zoo_id" IN (1, 3, 2)) ORDER BY "id"

DataMapper2 requests

Lazy loading + SEL

DataMapper i n 2 0 m i n Lazy load

DM doesn’t load all properties for each request

unless required

DataMapper i n 2 0 m i n Multiple Repos

production: adapter: mysql encoding: utf8 database: production-app username: root password: top-s3ckit host: localhost repositories: nightly_backup: adapter: sqlite3 database: shared/nightly.db weekly_backup: adapter: sqlite3 database: shared/weekly.db

data

base

.ym

l

DataMapper i n 2 0 m i n Multiple Repos

Article.copy(:default, :nightly_backup, :created.gt => 1.day.ago )

DataMapper i n 2 0 m i n Multiple Repos

Article.copy(:nightly_backup, :weekly_backup, :created.gt => 1.week.ago )

DataMapper i n 2 0 m i n Legacy Data

class Page include DataMapper::Resource property :id, Serial property :name, String repository(:legacy) do property :name, String, :field => "title" end

DataMapper i n 2 0 m i n Adapters

Provide an interface between your models

and a data store.

DataMapper i n 2 0 m i n Adapters

imapfile system

salesforce APIREST

couchdbferret

google video - http scraper

DataMapper i n 2 0 m i n Lazy Attributes

google video - http scraperDM adapter

80 LOC

only required to implement - a read method

- handling query conditions

DataMapper i n 2 0 m i n Lazy Attributes

Sales Force read-writeDM adapter

200 LOC

DataMapper i n 2 0 m i n Migrations

automigrateautoupdatemigration

DataMapper i n 2 0 m i n Custom Types

Load / Dump

DataMapper i n 2 0 m i n

modular structure

Plugins

migration, validation, model factory, state machine, constraints, lists, tagging...

DM facilitates extensions by offering nice hooks

DataMapper i n 2 0 m i n

Person.all(Person.addresses.street.like => "%street%" )

SELECT "people"."id", "people"."name" FROM "people"INNER JOIN "addresses" ON ("people"."id" = "addresses"."person_id")WHERE ("addresses"."street" LIKE '%street%')ORDER BY "people"."id"

Query::Path

Find all people with an address that has street in the street name

DataMapper i n 2 0 m i n

Person.all("addresses.street.like" => "%street%" )

SELECT "people"."id", "people"."name" FROM "people"INNER JOIN "addresses" ON ("people"."id" = "addresses"."person_id")WHERE ("addresses"."street" LIKE '%street%')ORDER BY "people"."id"

Query::Path

Find all people with an address that has street in the street name

DataMapper i n 2 0 m i n

people who helped with this presentation:

Credits

dbussink, dkubb, afrench, wycats