Class: Quarantine::Databases::DynamoDB
- Defined in:
- lib/quarantine/databases/dynamo_db.rb
Instance Attribute Summary collapse
-
#dynamodb ⇒ Object
Returns the value of attribute dynamodb.
Instance Method Summary collapse
- #batch_write_item(table_name, items, additional_attributes = {}) ⇒ Object
- #create_table(table_name, attributes, additional_arguments = {}) ⇒ Object
- #delete_item(table_name, keys) ⇒ Object
-
#initialize(aws_region: 'us-west-1', **_additional_arguments) ⇒ DynamoDB
constructor
A new instance of DynamoDB.
- #scan(table_name) ⇒ Object
Constructor Details
#initialize(aws_region: 'us-west-1', **_additional_arguments) ⇒ DynamoDB
Returns a new instance of DynamoDB.
10 11 12 |
# File 'lib/quarantine/databases/dynamo_db.rb', line 10 def initialize(aws_region: 'us-west-1', **_additional_arguments) @dynamodb = Aws::DynamoDB::Client.new({ region: aws_region }) end |
Instance Attribute Details
#dynamodb ⇒ Object
Returns the value of attribute dynamodb.
8 9 10 |
# File 'lib/quarantine/databases/dynamo_db.rb', line 8 def dynamodb @dynamodb end |
Instance Method Details
#batch_write_item(table_name, items, additional_attributes = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/quarantine/databases/dynamo_db.rb', line 24 def batch_write_item(table_name, items, additional_attributes = {}) dynamodb.batch_write_item( { request_items: { table_name => items.map do |item| { put_request: { item: { **item.to_hash, **additional_attributes } } } end } } ) rescue Aws::DynamoDB::Errors::ServiceError raise Quarantine::DatabaseError end |
#create_table(table_name, attributes, additional_arguments = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/quarantine/databases/dynamo_db.rb', line 53 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 |
#delete_item(table_name, keys) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/quarantine/databases/dynamo_db.rb', line 40 def delete_item(table_name, keys) dynamodb.delete_item( { table_name: table_name, key: { **keys } } ) rescue Aws::DynamoDB::Errors::ServiceError raise Quarantine::DatabaseError end |
#scan(table_name) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/quarantine/databases/dynamo_db.rb', line 14 def scan(table_name) begin result = dynamodb.scan({ table_name: table_name }) rescue Aws::DynamoDB::Errors::ServiceError raise Quarantine::DatabaseError end result&.items end |