How can I access more than one database with Hanami/Repository

653 Views Asked by At

I have a application that has to generate reports from a data warehouse.
I don't want to save the app specific data (user, permissions, report defintions, ...) in the same database where the warehouse lives. Also, in the future it is very likely that I have to get access to other databases as well (maybe even Oracle).
I have configured my database connection in the .env files but I don't know if or where I can define a new connection and also how to instanciate a repository for this explicit connection.
Since this is the 2nd container from my app, I was wondering if it is possible to change the orm for one container, when hanami-model isn't suited for my needs...

2

There are 2 best solutions below

0
On BEST ANSWER

This is an example how to connect to two databases using hanami:

require 'pg'
require 'hanami/model'
require 'hanami/model/adapters/sql_adapter'

mapper = Hanami::Model::Mapper.new do
  # ...
end

adapter1 = Hanami::Model::Adapters::SqlAdapter.new(mapper, 'postgres://host:port/database1')

adapter2 = Hanami::Model::Adapters::SqlAdapter.new(mapper, 'postgres://host:port/database2')


DataRepository.adapter  = adapter1
UserRepository.adapter = adapter2
0
On

I'm sorry, you can't connect to more than one database per project at this time. We're evaluating this feature after 1.0.