rom 4.0 is coming

rom 4.0 is coming

E864e5088627498df8f9b911a9bc3219?s=128

Piotr Solnica

July 09, 2017
Tweet

Transcript

  1. ROM-RB 4.0 IS COMING PIOTR SOLNICA >> BRIGHTON RUBY 2017

    1
  2. PIOTR SOLNICA > ! Cracow, Poland > github.com/solnic > @_solnic_

    > solnic.eu 2
  3. TECH LEAD AT ICELAB 3

  4. WHAT'S ROM-RB? 4

  5. an open-source persistence and mapping toolkit for Ruby built for

    speed and simplicity 5
  6. DATABASE-AGNOSTIC, FLEXIBLE, EXTENDIBLE, FAST, SIMPLE 6

  7. FOR REAL ! 7

  8. SOUNDS LIKE SOME CRAZY OBJECT RELATIONAL MAPPER, EH? 8

  9. ROM IS NOT AN ORM 9

  10. WHY DOES IT EVEN EXIST? 10

  11. A TRUE ALTERNATIVE TO ACTIVE RECORD 11

  12. ROM-RB PROVIDES A WAY TO SEPARATE PERSISTENCE CONCERNS FROM APPLICATION

    DOMAIN 12
  13. WHAT'S THE DEAL WITH 4.0? 13

  14. RUBYISTS LOVE MAGIC ˾(✿˙ᗜ˙)੭ʓ̤ꚍ.*ꙓꙏꚍ 14

  15. MAGIC AKA: IMPLICIT CODE AKA: EASY TO USE ABSTRACTIONS 15

  16. ACTIVERECORD POSSIBLY THE MOST MAGICAL GEM IN THE UNIVERSE 16

  17. NO BOILERPLATE class User < ActiveRecord::Base end 17

  18. PERSISTENCE, SO SIMPLE user = User.create(name: "Jane") 18

  19. QUERIES, SO SIMPLE User.where(name: "Jane").first 19

  20. MAKING CHANGES, SO SIMPLE User.where(name: "Jane").update_all(name: "Jane Doe") 20

  21. EASE OF USE > Little code to write to get

    started > A lot of functionality OOTB > No boilerplate 21
  22. THIS IS A REAL CHALLENGE FOR ROM-RB! 22

  23. > Dynamic query interface > Explicit representation of data structures

    > Mapping to struct objects decoupled from the database > No concept of lazy-loadable associations 23
  24. THIS IS FINE 24

  25. WE MADE IT (⊃ꙏ•́‿•̀ꙏ)⊃━☆ꚍ.*ꙓꙏꚍ 25

  26. RELATIONS AND STRUCTS 26

  27. class Users < ROM::Relation[:sql] schema(infer: true) end 27

  28. ✅ NO BOILERPLATE 28

  29. users.changeset(:create, name: "Jane").commit # => #<ROM::Struct::User id=1 name="Jane"> 29

  30. ✅ PERSISTENCE, SO SIMPLE 30

  31. users.where(name: "Jane").first # => #<ROM::Struct::User id=1 name="Jane"> 31

  32. ✅ QUERIES, SO SIMPLE 32

  33. users.where(name: "Jane").changeset(:update, name: "Jane Doe").commit # => #<ROM::Struct::User id=1 name="Jane

    Doe"> 33
  34. ✅ MAKING CHANGES, SO SIMPLE 34

  35. BUT... 35

  36. users.first # => #<ROM::Struct::User id=1 name="Jane"> users.select(:name).first # => #<ROM::Struct::User

    name="Jane"> 36
  37. PEOPLE WANT THEIR OWN METHODS, OBVIOUSLY 37

  38. class User < ActiveRecord::Base def first_name name.split(' ').first end def

    last_name name.split(' ').last end end 38
  39. ROM-RB: CUSTOM STRUCT NAMESPACE 39

  40. class Users < ROM::Relation[:sql] struct_namespace Entities schema(infer: true) end 40

  41. class Users < ROM::Relation[:sql] struct_namespace Entities schema(infer: true) end 41

  42. module Entities class User < ROM::Struct def first_name name.split(' ').first

    end def last_name name.split(' ').last end end end 42
  43. module Entities class User < ROM::Struct def first_name name.split(' ').first

    end def last_name name.split(' ').last end end end 43
  44. user = users.first => #<Entities::User id=1 name="Jane Doe"> user.first_name #

    "Jane" user.last_name # "Doe" 44
  45. user = users.select(:name).first => #<Entities::User name="Jane Doe"> user.first_name # "Jane"

    user.last_name # "Doe" 45
  46. WHAT DOES THIS MEAN, REALLY? ! 46

  47. THE TRUTH ABOUT MAGIC 47

  48. EXCITEMENT ABOUT MAGIC (AKA HAPPINESS) 48

  49. ROM-RB DOESN'T ENFORCE MAGIC! 49

  50. AT ANY POINT IN TIME YOU CAN DEFINE STRUCTS WITH

    EXPLICIT ATTRIBUTES AND ASK ROM-RB TO LOAD THEM 50
  51. require "entities/address" module Entities class UserProfile < ROM::Struct attribute :email,

    Types::Strict::String attribute :name, Types::Strict::String attribute :age, Types::Strict::Integer attribute :address, Address end end 51
  52. users.combine(:address).as(:user_profile).first 52

  53. HOWEVER...THIS REQUIRES TIME, AND GOOD UNDERSTANDING OF THE APPLICATION DOMAIN!

    53
  54. IT'S A PROCESS, AND ROM-RB FULLY SUPPORTS IT 54

  55. 4.0.0.BETA WAS RELEASED A COUPLE WEEKS AGO ! (JUST SAYING!)

    55
  56. THANK YOU ! ! GITHUB.COM/ROM-RB/ROM 56