wroc_love.rb-beyond-the-orm-an-introduction-to-datamapper-2.pdf

 wroc_love.rb-beyond-the-orm-an-introduction-to-datamapper-2.pdf

An introduction to the core elements that will be part of the DataMapper 2 project - an implementation of the Data Mapper pattern in Ruby language.

E864e5088627498df8f9b911a9bc3219?s=128

Piotr Solnica

March 11, 2012
Tweet

Transcript

  1. Beyond the ORM an introduction to DataMapper 2 Sunday, March

    11, 12
  2. Piotr Solnica • codebenders.com • solnic.eu • github.com/solnic • @_solnic_

    Sunday, March 11, 12
  3. Before I start talking about DataMapper... Sunday, March 11, 12

  4. Active Record ...the pattern Sunday, March 11, 12

  5. “An object that wraps a row in a database table

    or view, encapsulates the database access, and adds domain logic on that data.” Martin Fowler Sunday, March 11, 12
  6. “An object that wraps a row in a database table

    or view, encapsulates the database access, and adds domain logic on that data.” Martin Fowler Sunday, March 11, 12
  7. “An object that wraps a row in a database table

    or view, encapsulates the database access, and adds domain logic on that data.” Martin Fowler Sunday, March 11, 12
  8. When should I use Active Record? Sunday, March 11, 12

  9. “Active Record is a good choice for domain logic that

    isn't too complex, such as creates, reads, updates and deletes” Martin Fowler Sunday, March 11, 12
  10. “Active Record is a good choice for domain logic that

    isn't too complex, such as creates, reads, updates and deletes” Martin Fowler Sunday, March 11, 12
  11. Data Mapper ...the pattern Sunday, March 11, 12

  12. “A layer of Mappers that moves data between objects and

    a database while keeping them independent of each other and the mapper itself.” Martin Fowler Sunday, March 11, 12
  13. “A layer of Mappers that moves data between objects and

    a database while keeping them independent of each other and the mapper itself.” Martin Fowler Sunday, March 11, 12
  14. When should I use Data Mapper? Sunday, March 11, 12

  15. “The primary occasion for using Data Mapper is when you

    want the database schema and the object model to evolve independently” Martin Fowler Sunday, March 11, 12
  16. “The primary occasion for using Data Mapper is when you

    want the database schema and the object model to evolve independently” Martin Fowler Sunday, March 11, 12
  17. Datamapper 2 Sunday, March 11, 12

  18. DataMapper 2 • Data Mapper pattern implementation • Based on

    a general-purpose relational algebra engine • Support different data stores via adapters • A set of independent libraries Sunday, March 11, 12
  19. Work in progress! we’re probably ~50% done Sunday, March 11,

    12
  20. New Libraries • “Veritas” - the new relational algebra engine

    • “Virtus” - model definition and introspection • “Aequitas” - validations Latin names are actually code-names :) Sunday, March 11, 12
  21. New core library • The mapper • Query API •

    Unit of Work • Interfaces for 3rd-party plugins Sunday, March 11, 12
  22. veritas Sunday, March 11, 12

  23. • Complete support for all relational algebra operations • All

    operations can be run in-memory • Can be extended to support any kind of a datastore • Can be used with multiple different databases • Designed to support per database optimizations Veritas Sunday, March 11, 12
  24. veritas & github api Sunday, March 11, 12

  25. Veritas github = Faraday.new(:url => 'https://api.github.com') members_body = github.get( '/orgs/datamapper/members').body

    commits_body = github.get( '/repos/datamapper/dm-core/commits').body members_json = JSON.parse(members_body) commits_json = JSON.parse(commits_body) Sunday, March 11, 12
  26. Veritas member_header = [ [ :id, Integer ], [ :login,

    String ], [ :gravatar_id, String ], [ :avatar_url, String ] ] commit_header = [ [ :committer, String ], [ :sha, String ], [ :message, String ], [ :date, DateTime ] ] Sunday, March 11, 12
  27. Veritas members_tuple = members_json.map do |member| [ member['id'], member['login'], member['gravatar_id'],

    member['avatar_url'] ] end commits_tuple = commits_json.map do |commit| [ commit['committer']['login'], commit['commit']['tree']['sha'], commit['commit']['message'], commit['committer']['date'] ] end Sunday, March 11, 12
  28. Veritas members_relation = Veritas::Relation::Base.new( 'members', member_header, members_tuple) commits_relation = Veritas::Relation::Base.new(

    'commits', commit_header, commits_tuple) members_relation.each do |member| puts member[:login] end commits_relation.each do |commit| puts commit[:sha] end Sunday, March 11, 12
  29. Veritas new_relation = members_relation.restrict do |r| r.login.eq('dkubb').or(r.login.eq('solnic')) end new_relation.each do

    |member| puts member[:login] end # dkubb # solnic Sunday, March 11, 12
  30. Veritas new_relation = members_relation.join(commits_relation) do |r| r.login.eq(r.committer) end new_relation.each do

    |member| puts "#{member[:id]} #{member[:login]} #{member[:sha]}" end # 133 dkubb 039c80242eaf4e7adc3775c2739813d1b20b2c3c # 7657 emmanuel e145b4fa7d5e44d983c7e2e990a9a6e4a4f912d3 # 2833 namelessjon becb114f6800e07e2d1941f6751501f43d2174ae # 1066 solnic b951892d442149a74f14480f9a6478a350585e85 # etc... Sunday, March 11, 12
  31. joining data from two different datastores? Sunday, March 11, 12

  32. Veritas PostgreSQL + Github API Sunday, March 11, 12

  33. Veritas CREATE TABLE "users" ( "id" SERIAL NOT NULL PRIMARY

    KEY, "username" VARCHAR(50) NOT NULL, "country" VARCHAR(2) NOT NULL ) INSERT INTO "users" ("id", "username", "country") VALUES (1, 'dkubb', 'CA') INSERT INTO "users" ("id", "username", "country") VALUES (2, 'solnic', 'PL') INSERT INTO "users" ("id", "username", "country") VALUES (3, 'john', 'UK') INSERT INTO "users" ("id", "username", "country") VALUES (4, 'jane', 'AU') Sunday, March 11, 12
  34. Veritas adapter = Veritas::Adapter::DataObjects.new( 'postgres://localhost/test' ) users_header = [ [

    :id, Integer ], [ :username, String ], [ :country, String ] ] Sunday, March 11, 12
  35. Veritas users_relation = Veritas::Relation::Gateway.new( adapter, Veritas::Relation::Base.new('users', users_header) ) Sunday, March

    11, 12
  36. Veritas # # SELECT "id", "username", “country” # FROM "users"

    # users_relation.each do |user| puts "#{user[:id]} #{user[:username]}" end Sunday, March 11, 12
  37. Veritas new_relation = users_relation.join( members_relation.rename( :login => :username ).remove([ :id

    ]) ) new_relation.each do |user| puts "#{user[:id]} #{user[:username]}" end # 1 dkubb # 2 solnic Sunday, March 11, 12
  38. Veritas restricted = users_relation.restrict do |r| r.country.eq('CA').or(r.country.eq('UK')) end new_relation =

    restricted.join( members_relation.rename( :login => :username).remove([ :id ] ) ) Sunday, March 11, 12
  39. Veritas # First in PostgreSQL: # # SELECT "id", "username",

    "country" FROM "users" # WHERE ("country" = 'US' OR "country" = 'UK') # # *Then in memory* # # join with the members from github on username/login # new_relation.each do |user| puts "#{user[:id]} #{user[:username]} #{user[:country]}" end # 1 dkubb CA Sunday, March 11, 12
  40. Virtus Sunday, March 11, 12

  41. Virtus • Attributes • Coercions • Embedded Values • Embedded

    Collections • Value Objects Sunday, March 11, 12
  42. Virtus class User include Virtus attribute :name, String attribute :age,

    Integer end user = User.new(:name => 'John', :age => '29') user.name # => "John" user.age # => 29 Sunday, March 11, 12
  43. Virtus class Book include Virtus attribute :title, String attribute :author,

    String end class Library include Virtus attribute :books, Set[Book] end Sunday, March 11, 12
  44. Virtus library = Library.new(:books => [ { :title => 'Some

    Book', :author => 'John Doe' } ]) library.books.class # => Set library.books.first.inspect # => #<Book:0x0000010884bbc0 @title="Some Book", @author="John Doe"> Sunday, March 11, 12
  45. Virtus Virtus::Coercion::Object.to_integer("1") # => 1 Virtus::Coercion::Object.to_string(1) # => "1" Virtus::Coercion::Object.to_array(1)

    # => [ 1 ] Sunday, March 11, 12
  46. Virtus Virtus::Coercion::String.to_date("2012/03/10") # => #<Date: 2012-03-10 ((2455997j,0s,0n),+0s,2299161j)> Virtus::Coercion::Date.to_string(Date.new(2012,3,10)) # =>

    "2012-03-10" Virtus::Coercion::Integer.to_float(1) # => 1.0 Sunday, March 11, 12
  47. missing pieces... Sunday, March 11, 12

  48. missing pieces • Mapper • Session / Unit of Work

    • Database schema reflection • Migrations • Constraints • Veritas Adapters Sunday, March 11, 12
  49. missing pieces - mapper https://github.com/solnic/dm-mapper class User attr_reader :id, :name

    def initialize(attributes) @id, @name = attributes.values_at(:id, :name) end class Mapper < DataMapper::VeritasMapper map :id, :type => Integer map :name, :to => :username, :type => String model User name 'users' end end User::Mapper.find(:name => 'John') Sunday, March 11, 12
  50. missing pieces - session https://github.com/solnic/dm-session library = Library.new(:books => [])

    book = Book.new(:title => 'Some Book', :author => 'Jane Doe') library.books << book DataMapper::Session.create do |session| session.insert(library) session.commit end Sunday, March 11, 12
  51. Resources • https://github.com/dkubb/veritas • https://github.com/solnic/virtus • https://github.com/emmanuel/aequitas • https://github.com/datamapper/dm-core/ wiki/Roadmap

    Sunday, March 11, 12
  52. thank you! Sunday, March 11, 12

  53. questions? Sunday, March 11, 12