Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DataMapper

 DataMapper

Presentation introducing DataMapper given at the RubyArgentina Meetup, 13 May 2011. (Based on a presentation by Dirkjan Bussink given at RubyEnRails Amsterdam 2009).

Avatar for Alex Coles

Alex Coles

March 31, 2012
Tweet

More Decks by Alex Coles

Other Decks in Programming

Transcript

  1. class  Person    include  DataMapper::Resource    property  :id,    

     Serial    property  :name,  String    property  :age,    Integer end Sonntag, 1. April 12
  2. Mapa de identidad repository  do    puts  Person.get(1).object_id  =>  2232427080

       puts  Person.get(1).object_id  =>  2232427080 end Sonntag, 1. April 12
  3. people  =  Person.all(:name.like  =>  "%me%") ~  Query  log  empty... p

     people ~  SELECT  "id",  "name"  FROM  people Sonntag, 1. April 12
  4. Carga por adelantado people  =  Person.all people.each  do  |person|  

     p  person.addresses end SELECT  "id",  "name"  FROM  people; SELECT  "id",  "street"  FROM  addresses  WHERE  ("person_id"  IN  (1,2,3)) Sonntag, 1. April 12
  5. Repositorios múltiples DataMapper.setup(:repo1,  "postgres://postgres@localhost/dm") DataMapper.setup(:repo2,  "mysql://root@localhost/dm") repository(:repo1)  do    p

     Person.get(1)  =>  #<Person  @id=1  @name="Piotr"  @age=20> end repository(:repo2)  do    p  Person.get(1)  =>  #<Person  @id=1  @name="José"  @age=12> end Sonntag, 1. April 12
  6. DataMapper.setup(:repo1, "postgres://postgres@localhost/dm") DataMapper.setup(:repo2, "mysql://root@localhost/dm") get "/" do # Odd /

    even sharding repo = "repo#{user.id % 2 + 1}".to_sym repository(repo) do @people = Person.all end erb :people end Sharding simple Sonntag, 1. April 12
  7. module  DataMapper    module  Adapters        class  MyStorageAdapter

     <  AbstractAdapter            def  create(resources)            end            def  read(query)            end            def  update(attributes,  collection)            end            def  delete(collection)            end        end    end end Sonntag, 1. April 12
  8. términos de búsqueda complejos! Person.all(Person.addresses.street.like  =>  "%street%")      

     SELECT  "people"."id",  "people"."name",  "people"."age"  FROM  "people" INNER  JOIN  "addresses"  ON  "people"."id"  =  "addresses"."person_id"          WHERE  "addresses"."street"  LIKE  '%street%'    GROUP  BY  "people"."id",  "people"."name",  "people"."age"    ORDER  BY  "people"."id" Sonntag, 1. April 12
  9. class Person include DataMapper::Resource property :id, Serial property :name, String

    property :age, Integer has n, :addresses def self.named_like_me all(:name.like => "%me%") end def self.older_than_me all(:age.gt => 27) end end Person.named_like_me.older_than_me ~ SELECT "id", "name", "age" FROM "people" WHERE ("name" LIKE '%me%' AND "age" > 27) ORDER BY "id" Encadenamiento de términos de búsqueda Sonntag, 1. April 12
  10. class Person include DataMapper::Resource property :id, Serial property :name, String

    property :age, Integer has n, :addresses def self.named_like_me all(:name.like => "%me%") end def self.older_than_me all(:age.gt => 27) end end Person.named_like_me.older_than_me ~ SELECT "id", "name", "age" FROM "people" WHERE ("name" LIKE '%me%' AND "age" > 27) ORDER BY "id" Encadenamiento de términos de búsqueda Alcances nominales en simple Ruby Sonntag, 1. April 12
  11. Subtérminos de búsqueda Person.all(:name.like  =>  '%me%').addresses.all(:street.not  =>  nil) SELECT  "id",

     "street",  "person_id"  FROM  "addresses"    WHERE  (                "person_id"  IN  (SELECT  "id"  FROM  "people"  WHERE  "name"  LIKE  '%me%')                  AND  "street"  IS  NOT  NULL  )  ORDER  BY  "id" Sonntag, 1. April 12
  12. Person.all  -­‐  Person.all(:name  =>  'Dan  Kubb') SELECT  *  FROM  people

     WHERE  NOT(name  =  'Dan  Kubb') Sonntag, 1. April 12
  13. [1,  2]  &  [2,  3]  ==  [2] [1,  2]  |

     [2,  3]  ==  [1,  2,  3] Sonntag, 1. April 12
  14. Validaciones automáticas class  Person    ...    property  :name,  String,

     :nullable  =>  false end CREATE  TABLE  "people"  ("id"  SERIAL  NOT  NULL,                                                "name"  VARCHAR(50)  NOT  NULL,                                              "age"  INTEGER,  PRIMARY  KEY("id")) Sonntag, 1. April 12
  15. DataObjects::IntegrityError:  ERROR:     null  value  in  column  "name"  violates

     not-­‐null   constraint Person.create Sonntag, 1. April 12
  16. person  =  Person.create p  person.errors  =>  #<DataMapper::Validate::ValidationErrors:0x101d217a0      

                                         @errors={:name=>["Name  must  not  be  blank"]},                                          @resource=#<Person  @id=nil  @name=nil  @age=nil>> Sonntag, 1. April 12
  17. Sello de tiempo class  Person    ...    property  :created_at,

     DateTime    property  :updated_at,  DateTime end Sonntag, 1. April 12
  18. Restricciones class  Person    ...    has  n,  :addresses,  :constraint

     =>  :destroy! end ALTER  TABLE  addresses ADD  CONSTRAINT  addresses_person_id_fk FOREIGN  KEY  (person_id) REFERENCES  people ON  DELETE  CASCADE ON  UPDATE  CASCADE Sonntag, 1. April 12
  19. module  DataMapper    module  Types        class  IPAddress

     <  DataMapper::Type            primitive  String            length        16            def  self.load(value,  property)                IPAddr.new(value)            end            def  self.dump(value,  property)                value.to_s            end            def  self.typecast(value,  property)                value.kind_of?(IPAddr)  ?  value  :  load(value,  property)            end        end    end end class  NetworkNode    ...    property  :ipaddress,  IPAddress end Tipos personalizables Sonntag, 1. April 12
  20. class  Address    include  DataMapper::EmbeddedValue    property  :street,    

           String end class  Person    include  DataMapper::Resource    property  :id,            Serial    property  :name,        String    has  n,  :addresses end Sonntag, 1. April 12
  21. Términos de búsqueda estilo Ambition User.select  {  |u|  u.id  ==

     1  &&  u.name  ==  'Dan  Kubb'  } Sonntag, 1. April 12
  22. 1.1.x Mapa de ruta • Resolver la mayoría de los

    bugs • Arreglar problemas con uniones (joins) complejas • Soporte para validaciones i18n • Encontrar un método conveniente para detectar (typecasting) valores en blanco para nils en dm- rails • Finalizar el soporte para propiedades binarias (que deberían mapear a BLOB, BYTEA, etc.) Sonntag, 1. April 12
  23. 1.2 Mapa de ruta • Propriedad API nalizada • EmbeddedValue

    • Hooks mejorados • Soporte de uniones (joins) explícitos • Soportar comando ordenar en asociaciones • Soporte de restricciones utilizando sqlite3 Sonntag, 1. April 12
  24. EmbeddedValue qué vendrá en 1.2 class  Person    include  Address

       property  :id,            Serial end class  Address    include  DataMapper::EmbeddedValue end Sonntag, 1. April 12
  25. 2.0 Mapa de ruta • Basado en patrones DataMapper verdaderos

    • Integración con Veritas Sonntag, 1. April 12