Class: Dinamo::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/dinamo/adapter.rb

Defined Under Namespace

Modules: Glue

Instance Method Summary collapse

Constructor Details

#initialize(table_name: nil, **options) ⇒ Adapter

Returns a new instance of Adapter.



17
18
19
20
21
# File 'lib/dinamo/adapter.rb', line 17

def initialize(table_name: nil, **options)
  @options    = options
  @database   = Aws::DynamoDB::Client.new
  @table_name = table_name
end

Instance Method Details

#delete(key) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dinamo/adapter.rb', line 60

def delete(key)
  find_or_abort!(**key)
  @database.delete_item(
    table_name: @table_name,
    key: key
  )
end

#exist?(**keys) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/dinamo/adapter.rb', line 74

def exist?(**keys)
  !!get(**keys).item rescue false
end

#get(**keys) ⇒ Object



23
24
25
26
27
# File 'lib/dinamo/adapter.rb', line 23

def get(**keys)
  @database.get_item(table_name: @table_name, key: keys)
rescue
  handle_error! $!
end

#insert(**keys) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/dinamo/adapter.rb', line 38

def insert(**keys)
  @database.put_item(
    table_name: @table_name,
    item: keys,
    return_values: "ALL_OLD"
  )
rescue
  handle_error! $!
end

#partition(conditions) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/dinamo/adapter.rb', line 29

def partition(conditions)
  @database.query(
    table_name: @table_name,
    key_conditions: make_key_conditions(**conditions)
  )
rescue
  handle_error! $!
end

#serialize_attributes(attributes) ⇒ Object



68
69
70
71
72
# File 'lib/dinamo/adapter.rb', line 68

def serialize_attributes(attributes)
  attributes.each_with_object({}) do |(key, value), new_attributes|
    new_attributes[key] = { value: value, action: "PUT" }
  end
end

#update(key, attributes) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dinamo/adapter.rb', line 48

def update(key, attributes)
  find_or_abort!(**key)
  @database.update_item(
    table_name: @table_name,
    key: key,
    attribute_updates: serialize_attributes(attributes),
    return_values: "ALL_NEW"
  )
rescue
  handle_error! $!
end