Sha256: 7d53c109922c3db2642d90e06a96c0ced33388a69d322b377e70f8266c4882a0

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require 'rom'
require 'rom-sql'

module PicoApi
  class Database
    class << self
      attr_accessor :container

      def setup!
        database_config = PicoApi.configuration.db_config.deep_symbolize_keys
        logger = PicoApi.configuration.logger
        gateways = database_config.keys

        configs = create_config(gateways, database_config)
        cont = ROM.container(**configs) { |config| yield config if block_given? }

        gateways.each { |gateway| cont.gateways[gateway].use_logger(logger) } if logger

        @container = cont
      end

      private

      def create_config(gateways, database_config)
        gateways.inject({}) do |memo, gateway|
          adapter = database_config.dig(gateway, :adapter)&.to_sym
          options = database_config.dig(gateway, :options)
          connection_string = database_config.dig(gateway, :connection_string)

          memo[gateway] = [adapter, connection_string, **options]
          memo
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pico_api-0.1.0 lib/pico_api/database.rb
pico_api-0.0.6 lib/pico_api/database.rb
pico_api-0.0.5 lib/pico_api/database.rb
pico_api-0.0.4 lib/pico_api/database.rb
pico_api-0.0.3 lib/pico_api/database.rb