Is rom-factory compatible with hamami?

Hi everyone

I am trying to config rom-factory.

I have created app and put provider

Hanami.app.register_provider :persistence, namespace: true do
prepare do
require ‘rom-changeset’
require ‘rom/core’
require ‘rom/sql’

config =
  ROM::Configuration.new(
    :sql, target['settings'].database_url
  )

register 'config', config
register 'db', config.gateways[:default].connection

end

start do
config = target[‘persistence.config’]

config.auto_registration(
  target.root.join('relations'),
  namespace: 'MyApp::Relations'
)

register 'rom', ROM.container(config)

end
end

also I configure factory

require “rom-factory”

require “faker”

Factory = ROM::Factory.configure do |config|
config.rom = MyApp::Container[“persistence.rom”]
end

Dir[File.dirname(FILE) + “/factories/*.rb”].each { |file| require file }

and created factory

Factory.define(:users) do |f|
f.sequence(:name) { |n| “name #{n}” }
end

but I got error

ROM::ElementNotFoundError:
:users doesn’t exist in ROM::RelationRegistry registry

Could you give me information how to config rom-factory correctly?

Hey Vladimir, are you using Hanami 2.2.0? I ask because setup in Hanami 2.1.0 was complicated but with 2.2.0 the Hanami DB gem is installed by default for you so you don’t need to do any of this because you can simply require ROM factory. In fact, that link is a working example as generated for you when building a new Hanami app using Hanamismith.

If you do that, you should be up and running in no time.

Hey @bkuhlmann
I use hanami 2.2.0.

I have several questions:

  1. Should I register separated provider for working with db?
  2. How to correctly register hanami relations with rom-factory?

I will check your link later. Thank you!

To answer:

  1. So that’s what I’m saying, you don’t need a ROM Factory provider at all. You’ll have what you need via the Hanami DB gem that is part of Hanami 2.2.0.
  2. Setting up and using relations is documented here.
1 Like

Brooke, thank you for your help!

Also I got my mistake

While configuring container I had to use

MyApp::Container["db.rom"]

instead of

MyApp::Container[“persistence.rom”]
1 Like

ROM::Factory integration didn’t get done in time for Hanami 2.2, but that will likely be released in the near future as a follow-on. In the meantime you have to do some manual configuration but that will be streamlined.

1 Like