Class: Quarantine::Databases::DynamoDB

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/quarantine/databases/dynamo_db.rb

Constant Summary collapse

Attribute =
T.type_alias { { attribute_name: String, attribute_type: String, key_type: String } }

Constants inherited from Base

Base::Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DynamoDB

Returns a new instance of DynamoDB.



21
22
23
24
25
# File 'lib/quarantine/databases/dynamo_db.rb', line 21

def initialize(options)
  super()

  @dynamodb = T.let(Aws::DynamoDB::Client.new(options), Aws::DynamoDB::Client)
end

Instance Attribute Details

#dynamodbObject

Returns the value of attribute dynamodb.



18
19
20
# File 'lib/quarantine/databases/dynamo_db.rb', line 18

def dynamodb
  @dynamodb
end

Instance Method Details

#create_table(table_name, attributes, additional_arguments = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/quarantine/databases/dynamo_db.rb', line 67

def create_table(table_name, attributes, additional_arguments = {})
  @dynamodb.create_table(
    {
      table_name: table_name,
      attribute_definitions: attributes.map do |attribute|
        {
          attribute_name: attribute[:attribute_name],
          attribute_type: attribute[:attribute_type]
        }
      end,
      key_schema: attributes.map do |attribute|
        {
          attribute_name: attribute[:attribute_name],
          key_type: attribute[:key_type]
        }
      end,
      **additional_arguments
    }
  )
rescue Aws::DynamoDB::Errors::ServiceError
  raise Quarantine::DatabaseError
end

#fetch_items(table_name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/quarantine/databases/dynamo_db.rb', line 28

def fetch_items(table_name)
  begin
    result = @dynamodb.scan(table_name: table_name)
  rescue Aws::DynamoDB::Errors::ServiceError
    raise Quarantine::DatabaseError
  end

  result.items
end

#write_items(table_name, items) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/quarantine/databases/dynamo_db.rb', line 44

def write_items(table_name, items)
  @dynamodb.batch_write_item(
    request_items: {
      table_name => items.map do |item|
        {
          put_request: {
            item: item
          }
        }
      end
    }
  )
rescue Aws::DynamoDB::Errors::ServiceError
  raise Quarantine::DatabaseError
end