Class: ActiveItem::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/active_item/transaction.rb

Overview

Wraps DynamoDB TransactWriteItems, allowing multiple put, update, and delete operations to be committed atomically (up to 100 items).

Constant Summary collapse

MAX_ITEMS =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



11
12
13
# File 'lib/active_item/transaction.rb', line 11

def initialize
  @operations = []
end

Instance Attribute Details

#operationsObject (readonly)

Returns the value of attribute operations.



9
10
11
# File 'lib/active_item/transaction.rb', line 9

def operations
  @operations
end

Instance Method Details

#delete(record) ⇒ Object



34
35
36
37
38
39
# File 'lib/active_item/transaction.rb', line 34

def delete(record)
  @operations << {
    op: { delete: { table_name: record.class.table_name, key: { record.class.primary_key.to_s => record.id } } },
    record: record, type: :delete
  }
end

#execute!Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_item/transaction.rb', line 82

def execute!
  return if @operations.empty?
  raise TransactionError, "DynamoDB transactions are limited to #{MAX_ITEMS} items (got #{@operations.length})" if @operations.length > MAX_ITEMS

  transact_items = @operations.map { |o| o[:op] }
  client = @operations.first[:record].class.dynamodb
  client.transact_write_items(transact_items: transact_items)

  @operations.each do |o|
    o[:record].instance_variable_set(:@new_record, false) if o[:type] == :put
  end
rescue Aws::DynamoDB::Errors::TransactionCanceledException => e
  raise TransactionError, "Transaction cancelled: #{e.message}"
rescue Aws::DynamoDB::Errors::ValidationException => e
  raise TransactionError, "Transaction validation failed: #{e.message}"
end

#put(record, condition: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_item/transaction.rb', line 15

def put(record, condition: nil)
  record.instance_variable_set(:@id, SecureRandom.uuid) unless record.id
  pk = record.class.primary_key
  record.instance_variable_set(:"@#{pk}", record.id) if pk != 'id'
  now = Time.now.utc.iso8601
  record.instance_variable_set(:@created_at, now) unless record.created_at
  record.instance_variable_set(:@updated_at, now)

  item = record.send(:build_dynamodb_item).merge(
    'createdAt' => record.created_at,
    'updatedAt' => record.updated_at
  )

  op = { put: { table_name: record.class.table_name, item: item } }
  op[:put][:condition_expression] = condition if condition

  @operations << { op: op, record: record, type: :put }
end

#update(record) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_item/transaction.rb', line 41

def update(record)
  changes = record.changes
  return if changes.empty?

  set_parts = []
  remove_parts = []
  attr_values = {}
  attr_names = {}

  changes.each_with_index do |(field, (_old_val, new_val)), idx|
    dynamo_key = record.class.to_dynamo_key(field)
    if new_val.nil?
      remove_parts << "#f#{idx}"
      attr_names["#f#{idx}"] = dynamo_key
    else
      set_parts << "#f#{idx} = :v#{idx}"
      attr_names["#f#{idx}"] = dynamo_key
      attr_values[":v#{idx}"] = new_val
    end
  end

  set_parts << 'updatedAt = :ts'
  attr_values[':ts'] = Time.now.utc.iso8601

  update_expression = "SET #{set_parts.join(', ')}"
  update_expression += " REMOVE #{remove_parts.join(', ')}" if remove_parts.any?

  @operations << {
    op: {
      update: {
        table_name: record.class.table_name,
        key: { record.class.primary_key.to_s => record.id },
        update_expression: update_expression,
        expression_attribute_names: attr_names.any? ? attr_names : nil,
        expression_attribute_values: attr_values
      }.compact
    },
    record: record, type: :update
  }
end