Last active
March 23, 2018 12:24
-
-
Save blelump/6782c5ce295213715a54455cf9744ce0 to your computer and use it in GitHub Desktop.
This file contains 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
module Entities | |
end | |
rom = ROM.container(:sql, 'sqlite::memory') do |conf| | |
conf.gateways[:default].use_logger(Logger.new($stdout)) | |
conf.default.create_table(:users) do | |
primary_key :id | |
column :login, String | |
end | |
conf.default.create_table(:employees_users) do | |
foreign_key :verifable_id, :employees | |
foreign_key :user_id, :users | |
end | |
conf.default.create_table(:employees) do | |
primary_key :id | |
column :name, String, null: false | |
end | |
conf.default.create_table(:roles_users) do | |
foreign_key :role_id, :roles | |
foreign_key :user_id, :users | |
end | |
conf.default.create_table(:roles) do | |
primary_key :id | |
column :name, String, null: false | |
end | |
conf.relation(:users) do | |
schema(:users, infer: true) do | |
associations do | |
has_many :roles_users | |
has_many :roles, through: :roles_users | |
has_many :employees_users | |
has_many :employees, through: :employees_users | |
end | |
end | |
end | |
conf.relation(:employees_users) do | |
schema(:employees_users, infer: true) do | |
associations do | |
belongs_to :user, foreign_key: :user_id | |
belongs_to :employee, foreign_key: :verifable_id | |
end | |
end | |
end | |
conf.relation(:employees) do | |
schema(:employees, infer: true) do | |
associations do | |
end | |
end | |
end | |
conf.relation(:roles_users) do | |
schema(:roles_users, infer: true) do | |
associations do | |
belongs_to :user | |
belongs_to :roles | |
end | |
end | |
end | |
conf.relation(:roles) do | |
schema(:roles, infer: true) do | |
end | |
end | |
end | |
class UserRepo < ROM::Repository[:users] | |
struct_namespace ::Entities | |
def by_id(id) | |
users.join(:employees_users).join(:employees, { id: :verifable_id }).select do |r| | |
[ | |
r[:users][:id].as(:user_id), | |
r[:employees][:id].as(:employee_id), | |
r[:employees][:name] | |
] | |
end.combine(:roles).to_a | |
end | |
end | |
employee = rom.relations[:employees].changeset(:create, name: 'Jane', id: 5).commit | |
rom.relations[:employees].changeset(:create, name: 'Jane', id: 8).commit | |
user = rom.relations[:users] | |
.changeset(:create, login: '[email protected]') | |
.commit | |
rom.relations[:employees_users] | |
.changeset(:create, user_id: user[:id], verifable_id: employee[:id]).commit | |
role = rom.relations[:roles] | |
.changeset(:create, name: 'a role').commit | |
rom.relations[:roles_users].changeset( | |
:create, | |
[ | |
{ role_id: role[:id], user_id: user[:id] }, | |
] | |
).commit | |
repo = UserRepo.new(rom) | |
user = repo.by_id(1) | |
p user | |
/work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:96:in `fetch': key not found: :id (KeyError) | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:96:in `block in pluck' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:48:in `block in each' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:48:in `each' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:48:in `each' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:96:in `map' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/loaded.rb:96:in `pluck' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-sql-2.3.0/lib/rom/sql/associations/core.rb:12:in `preload' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation.rb:318:in `preload_assoc' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/auto_curry.rb:48:in `block (2 levels) in auto_curry' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/curried.rb:59:in `call' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/combined.rb:60:in `block in call' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/combined.rb:60:in `map' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/combined.rb:60:in `call' | |
from /work/.rvm/gems/ruby-2.4.1@test_stuff/gems/rom-core-4.1.0/lib/rom/relation/materializable.rb:13:in `to_a' | |
from mapper3.rb:86:in `by_id' | |
from mapper3.rb:112:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment