module Dynamodb::TableActions

Public Instance Methods

_query(opts) click to toggle source
# File lib/dynamodb/table_actions.rb, line 34
def _query(opts)
  result = client.query(opts)
  result.items = result.items.map { |x| item_to_object(x) }
  result
end
create_table(_table_name, options) click to toggle source
# File lib/dynamodb/table_actions.rb, line 55
def create_table(_table_name, options)
  # To prevent accidentally deleting tables in production
  raise 'Can not create tables' unless Dynamodb.configuration.can_create_tables

  resource.create_table(
    {
      table_name: _table_name,
      provisioned_throughput: {
        read_capacity_units: 5,
        write_capacity_units: 5,
      }
    }.merge!(options)
  )
end
delete_item(_table_name, _key, options = {}) click to toggle source
# File lib/dynamodb/table_actions.rb, line 25
def delete_item(_table_name, _key, options = {})
  client.delete_item(
    {
      table_name: _table_name,
      key: _key
    }.merge!(options)
  )
end
delete_table(_table_name) click to toggle source
# File lib/dynamodb/table_actions.rb, line 48
def delete_table(_table_name)
  # To prevent accidentally deleting tables in production
  raise 'Can not delete tables' unless Dynamodb.configuration.can_delete_tables

  client.delete_table(table_name: _table_name)
end
describe_table(_table_name) click to toggle source
# File lib/dynamodb/table_actions.rb, line 40
def describe_table(_table_name)
  client.describe_table(table_name: _table_name)
end
get_item(_table_name, _key, options = {}) click to toggle source
# File lib/dynamodb/table_actions.rb, line 5
def get_item(_table_name, _key, options = {})
  _item = client.get_item(
    {
      table_name: _table_name,
      key: _key
    }.merge!(options)
  )[:item]

  _item ? item_to_object(_item) : nil
end
list_tables() click to toggle source
# File lib/dynamodb/table_actions.rb, line 44
def list_tables
  client.list_tables.table_names
end
put_item(_table_name, _item, options = {}) click to toggle source
# File lib/dynamodb/table_actions.rb, line 16
def put_item(_table_name, _item, options = {})
  client.put_item(
    {
      table_name: _table_name,
      item: _item
    }.merge!(options)
  )
end

Private Instance Methods

item_to_object(_item) click to toggle source

Converts a hash item result from dynamodb to object

# File lib/dynamodb/table_actions.rb, line 73
def item_to_object(_item)
  self.new(_item, false) # new_record false for existing records
end