Module: DynamodbRecord::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Document
Defined in:
lib/dynamodb_record/persistence.rb

Overview

Persistence Module

Instance Method Summary collapse

Instance Method Details

#destroyObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dynamodb_record/persistence.rb', line 125

def destroy
  options = self.class.default_options
  hash_key = self.class.hash_key
  range_key = self.class.range_key
  key = {}
  key[hash_key] = self.class.dump_field(read_attribute(hash_key), self.class.attributes[hash_key])

  if range_key.present?
    key[range_key] = self.class.dump_field(read_attribute(range_key), self.class.attributes[range_key])
  end

  self.class.client.delete_item(options.merge(key:))
end

#saveObject



100
101
102
103
104
105
# File 'lib/dynamodb_record/persistence.rb', line 100

def save
  save!
  true
rescue StandardError
  false
end

#save!Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dynamodb_record/persistence.rb', line 107

def save!
  options = self.class.default_options

  self.id = SecureRandom.uuid if id.nil?
  time = DateTime.now.to_s
  self.created_at = time if created_at.nil?
  self.updated_at = time

  if @new_record # New item. Don't overwrite if id exists
    options.merge!(condition_expression: 'id <> :s', expression_attribute_values: {':s' => id})
  end

  options.merge!(item: self.class.unload(attributes))
  # puts options
  self.class.client.put_item(options)
  @new_record = false
end