Apologies for the confusion here, I don’t think the Repository
base class will work with injection, at least I can’t get it to do so. As I and @katafrakt have done, it looks like ROM::Repository::Root
is the desirable base class here.
So try this:
module Admin
class Repository < ROM::Repository::Root
include Deps[container: "persistence.rom"]
end
module Repositories
class DoctorRepository < Repository[:doctors]
commands :create, update: :by_pk, delete: :by_pk
end
end
end
The [:doctors]
part refers to a registered relation name in ROM, so you should see it as Hanami.app["persistence.rom"].relations[:doctors]
Here’s a working test script:
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'sqlite3'
gem 'rom-sql'
gem 'dry-core'
gem 'dry-auto_inject'
end
require 'rom'
require 'rom/sql'
rom_config = ROM::Configuration.new(:sql, 'sqlite::memory')
gateway = rom_config.default_gateway
migration = gateway.migration do
change do
create_table :users do
primary_key :id
column :name, String, null: false
column :email, String, null: false
end
end
end
migration.apply(gateway.connection, :up)
class Users < ROM::Relation[:sql]
schema(infer: true)
end
rom_config.register_relation(Users)
container = Dry::Core::Container.new
container.register("persistence.rom", ROM.container(rom_config))
Deps = Dry::AutoInject(container)
class Repository < ROM::Repository::Root
include Deps[container: "persistence.rom"]
def find(id) = root.by_pk(id).one
end
class UserRepo < Repository[:users]
commands :create, update: :by_pk, delete: :by_pk
end
repo = UserRepo.new
repo.create(name: "Joe Example", email: "joe@example.com")
puts repo.find(1).inspect
# => #<ROM::Struct::User id=1 name="Joe Example" email="joe@example.com">