Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Datamapper @ Railsconf2010

3,557 views

Published on

DataMapper 1.0 presentation at Railsconf 2010

Published in: Technology, Business

Datamapper @ Railsconf2010

  1. 1. Dirkjan Bussink http://github.com/dbussink @dbussink
  2. 2. Why?
  3. 3. Declarative
  4. 4. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer end
  5. 5. Automatic migrations
  6. 6. DataMapper.auto_migrate! DataMapper.auto_upgrade!
  7. 7. Storage constraints
  8. 8. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer end
  9. 9. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer end CREATE TABLE "people" ("id" SERIAL NOT NULL, "name" VARCHAR(50) NOT NULL, "age" INTEGER, PRIMARY KEY("id"))
  10. 10. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer has n, :addresses, :constraint => :destroy! end
  11. 11. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer 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
  12. 12. Automatic validations
  13. 13. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer end
  14. 14. class Person include DataMapper::Resource property :id, Serial property :name, String, :required => true property :age, Integer end person = Person.create person.errors => #<DataMapper::Validations::ValidationErrors:0x101d217a0 @errors={:name=>["Name must not be blank"]}, @resource=#<Person @id=nil @name=nil @age=nil>>
  15. 15. http://www.flickr.com/photos/jon_truran
  16. 16. Convenience
  17. 17. "that just pisses me off"
  18. 18. Lazyness
  19. 19. people = Person.all ~ Query log empty...
  20. 20. people = Person.all ~ Query log empty... p people ~ SELECT "id", "name" FROM people
  21. 21. :each
  22. 22. people = Person.all people.each do |person| p person.addresses end
  23. 23. 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))
  24. 24. Complex queries
  25. 25. Person.all(:addresses => {:street.like => "%street%"})
  26. 26. Person.all(: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"
  27. 27. class Person include DataMapper::Resource ... def self.named_like_me all(:name.like => "%me%") end end
  28. 28. class Person include DataMapper::Resource ... def self.named_like_me all(:name.like => "%me%") end end Person.named_like_me.all(:age.gt => 27) ~ SELECT "id", "name", "age" FROM "people" WHERE ("name" LIKE '%me%' AND "age" > 27) ORDER BY "id"
  29. 29. Person.all(:name.like => '%me%').addresses.all(:street.not => nil)
  30. 30. 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"
  31. 31. Person.all - Person.all(:name => 'Dan Kubb')
  32. 32. Person.all - Person.all(:name => 'Dan Kubb') SELECT * FROM people WHERE NOT(name = 'Dan Kubb')
  33. 33. Not SQL centric
  34. 34. DataMapper.setup(:default, "postgres://postgres@localhost/dm") DataMapper.setup(:docs, "mongo://user@localhost/dm")
  35. 35. class Post include DataMapper::Resource has n, :audits end class Audit include DataMapper::Resource def self.default_repository_name :log end end
  36. 36. Custom adapters
  37. 37. 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
  38. 38. Future
  39. 39. class Address include DataMapper::EmbeddedValue property :street, String end class Person include DataMapper::Resource property :id, Serial property :name, String has n, :addresses end
  40. 40. drops into2008 Back to Rails 1.0
  41. 41. 1.8 / 1.9 (on Windows too)
  42. 42. Dan Kubb Martin Gamsjaeger
  43. 43. http://datamapper.org/ http://github.com/datamapper @datamapper http://groups.google.com/group/datamapper irc://irc.freenode.net/#datamapper

×