Created
February 20, 2017 14:30
-
-
Save flash-gordon/03dee95eebe69764cc0b89a1399f2e4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rom-sql' | |
require 'rom-repository' | |
rom = ROM.container(:sql, 'sqlite::memory') do |c| | |
c.gateways[:default].create_table :events do | |
primary_key :id | |
column :event_no, Integer | |
end | |
c.gateways[:default].create_table :orders do | |
primary_key :id | |
column :event_id, Integer | |
column :name, String | |
end | |
c.gateways[:default].use_logger(Logger.new($stdout)) | |
c.relation(:events) do | |
schema(infer: true) do | |
associations do | |
has_many :orders | |
end | |
end | |
end | |
c.relation(:orders) do | |
schema(infer: true) do | |
associations do | |
belongs_to :event | |
end | |
end | |
end | |
end | |
class OrderRepo < Class.new(ROM::Repository::Root)[:orders] | |
relations :events | |
def with_event | |
aggregate(:event).to_a | |
end | |
end | |
rom.relations[:events].insert event_no: 1 | |
rom.relations[:events].insert event_no: 2 | |
rom.relations[:orders].insert(name: 'Order #1', event_id: 1) | |
rom.relations[:orders].insert(name: 'Order #2', event_id: 1) | |
repo = OrderRepo.new(rom) | |
require "pry" | |
binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment