How to make global provider available to slice?

Hey,

I have a global orm-provider

# config/providers/orm.rb
Hanami.app.register_provider(:orm) do
  prepare do
    require "sequel"
  end

  start do
    register("orm.database", Sequel.connect(target["settings"].database_url))
  end
end

Furthermore I have a slice with a repo and I want to include the orm.database

module MySlice
  module Repositories
    class UsersRepo
      include Deps["orm.database"]
    end
  end
end

This fails with a

Dry::Core::Container::KeyError:
  key not found: "orm.database"

It is clear to me that this is the expected behavior, because slices have their own container, but what is the right way to make the “orm.database” key available to the slice?

I tried something like this:

module MySlice
  class Slice < Hanami::Slice
    import keys: ["orm.database"] from: :main # does not work
    MySlice::Slice.register("orm.database", Hanami.app["orm.database"]) # seems to work, but is this the right way
  end
end

The second approach seems to work, but is this the right way?
And what’s the reason do not make global keys available to all slices?
Thanks

Hey @wuarmin!

What you’ll want to use is shared_app_component_keys (see docs). Put this inside your app class in config/app.rb:

config.shared_app_component_keys += ["orm.database"]

This will make the "orm.database" component available in every slice.

3 Likes

Hey @timriley,
wow that’s awesome! I have to say what you have achieved over the past few years is truly incredible. Hanami 2 is really well thought out down to the last detail. I’m excited. Great DX.
Thanks

2 Likes