Class: Kvom::Adapter::DynamodbDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/kvom/adapter/dynamodb_document.rb

Instance Attribute Summary

Attributes inherited from Document

#key, #persisted_revision

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

#[], #[]=, #update_revision

Constructor Details

#initialize(key, attributes, item, from_db = false) ⇒ DynamodbDocument

Returns a new instance of DynamodbDocument.



15
16
17
18
# File 'lib/kvom/adapter/dynamodb_document.rb', line 15

def initialize(key, attributes, item, from_db = false)
  @dynamo_item = item
  super(key, attributes, from_db)
end

Class Method Details

.document_from_query(key, item_attributes, item) ⇒ Object



7
8
9
10
11
# File 'lib/kvom/adapter/dynamodb_document.rb', line 7

def document_from_query(key, item_attributes, item)
  item_attributes.delete("hash_key")
  item_attributes.delete("range_key")
  new(key, item_attributes, item, true)
end

Instance Method Details

#destroyObject



33
34
35
36
37
# File 'lib/kvom/adapter/dynamodb_document.rb', line 33

def destroy
  @dynamo_item.delete(revision_condition)
rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException => e
  raise Kvom::WriteConflict.wrap(e)
end

#writeObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kvom/adapter/dynamodb_document.rb', line 20

def write
  dynamo_attributes = attributes.to_h.merge({
    "hash_key" => @dynamo_item.hash_value,
    "range_key" => @dynamo_item.range_value,
  })
  begin
    # might be improved to saving only changed attributes
    @dynamo_item.table.items.put(dynamo_attributes, revision_condition)
  rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException => e
    raise Kvom::WriteConflict.wrap(e)
  end
end