Module: Dynomite::DbConfig::ClassMethods

Defined in:
lib/dynomite/db_config.rb

Constant Summary collapse

@@db =
nil

Instance Method Summary collapse

Instance Method Details

#check_dynamodb_local!(endpoint) ⇒ Object

When endoint has been configured to point at dynamodb local: localhost:8000 check if port 8000 is listening and timeout quickly. Or else it takes a for DynamoDB local to time out, about 10 seconds… This wastes less of the users time.



43
44
45
46
47
48
49
50
# File 'lib/dynomite/db_config.rb', line 43

def check_dynamodb_local!(endpoint)
  return unless endpoint && endpoint.include?("8000")

  open = port_open?("127.0.0.1", 8000, 0.2)
  unless open
    raise "You have configured your app to use DynamoDB local, but it is not running.  Please start DynamoDB local. Example: brew cask install dynamodb-local && dynamodb-local"
  end
end

#dbObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dynomite/db_config.rb', line 22

def db
  return @@db if @@db

  config = db_config
  endpoint = ENV['DYNAMODB_ENDPOINT'] || config['endpoint']
  check_dynamodb_local!(endpoint)

  # Normally, do not set the endpoint to use the current configured region.
  # Probably want to stay in the same region anyway for db connections.
  #
  # List of regional endpoints: https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region
  # Example:
  #   endpoint: https://dynamodb.us-east-1.amazonaws.com
  options = endpoint ? { endpoint: endpoint } : {}
  @@db ||= Aws::DynamoDB::Client.new(options)
end

#db=(db) ⇒ Object

useful for specs



68
69
70
# File 'lib/dynomite/db_config.rb', line 68

def db=(db)
  @@db = db
end

#db_configObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dynomite/db_config.rb', line 72

def db_config
  return @db_config if @db_config

  if defined?(Jets)
    config_path = "#{Jets.root}config/dynamodb.yml"
    env = Jets.env
  else
    config_path = ENV['DYNOMITE_CONFIG'] || "./config/dynamodb.yml"
    env = ENV['DYNOMITE_ENV'] || "development"
  end

  config = YAML.load(Dynomite::Erb.result(config_path))
  @db_config ||= config[env] || {}
end

#get_table_namespaceObject



96
97
98
99
100
101
# File 'lib/dynomite/db_config.rb', line 96

def get_table_namespace
  return @table_namespace if defined?(@table_namespace)

  config = db_config
  @table_namespace = config['table_namespace']
end

#port_open?(ip, port, seconds = 1) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dynomite/db_config.rb', line 53

def port_open?(ip, port, seconds=1)
  # => checks if a port is open or not
  Timeout::timeout(seconds) do
    begin
      TCPSocket.new(ip, port).close
      true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
      false
    end
  end
rescue Timeout::Error
  false
end

#set_table_namespace(value) ⇒ Object



103
104
105
# File 'lib/dynomite/db_config.rb', line 103

def set_table_namespace(value)
  @table_namespace = value
end

#table_namespace(*args) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dynomite/db_config.rb', line 87

def table_namespace(*args)
  case args.size
  when 0
    get_table_namespace
  when 1
    set_table_namespace(args[0])
  end
end