Class: Kcl::Proxies::DynamoDbProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/kcl/proxies/dynamo_db_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DynamoDbProxy

Returns a new instance of DynamoDbProxy.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 7

def initialize(config)
  @client = Aws::DynamoDB::Client.new(
    {
      access_key_id: config.aws_access_key_id,
      secret_access_key: config.aws_secret_access_key,
      region: config.aws_region,
      endpoint: config.dynamodb_endpoint,
      ssl_verify_peer: config.use_ssl
    }
  )
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 5

def client
  @client
end

Instance Method Details

#conditional_update_item(table_name, item, condition_expression, expression_attributes) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 103

def conditional_update_item(table_name, item, condition_expression, expression_attributes)
  @client.put_item(
    {
      table_name: table_name,
      item: item,
      condition_expression: condition_expression,
      expression_attribute_values: expression_attributes
    }
  )
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end

#create_table(table_name, attributes = [], schema = [], throughputs = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 32

def create_table(table_name, attributes = [], schema = [], throughputs = {})
  @client.create_table(
    {
      table_name: table_name,
      attribute_definitions: attributes,
      key_schema: schema,
      provisioned_throughput: throughputs
    }
  )
end

#delete_table(table_name) ⇒ Object



44
45
46
47
48
49
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 44

def delete_table(table_name)
  @client.delete_table({ table_name: table_name })
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end

#exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 20

def exists?(table_name)
  @client.describe_table({ table_name: table_name })
  true
rescue Aws::DynamoDB::Errors::NotFound,
       Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end

#get_item(table_name, conditions) ⇒ Hash

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 54

def get_item(table_name, conditions)
  response = @client.get_item(
    {
      table_name: table_name,
      key: conditions
    }
  )
  response.item
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  nil
end

#put_item(table_name, item) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 69

def put_item(table_name, item)
  @client.put_item(
    {
      table_name: table_name,
      item: item
    }
  )
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end

#remove_item(table_name, conditions) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 120

def remove_item(table_name, conditions)
  @client.delete_item(
    {
      table_name: table_name,
      key: conditions
    }
  )
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end

#update_item(table_name, conditions, update_expression) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kcl/proxies/dynamo_db_proxy.rb', line 85

def update_item(table_name, conditions, update_expression)
  @client.update_item(
    {
      table_name: table_name,
      key: conditions,
      update_expression: update_expression
    }
  )
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end