Module: Aws::Record::ItemOperations::ItemOperationsClassMethods

Included in:
Aws::Record
Defined in:
lib/aws-record/record/item_operations.rb

Instance Method Summary collapse

Instance Method Details

#find(opts) ⇒ Aws::Record

Returns builds and returns an instance of your model.

Examples:

Usage Example

class MyModel
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name, range_key: true
end

MyModel.find(id: 1, name: "First")

Parameters:

  • opts (Hash)

    attribute-value pairs for the key you wish to search for.

Returns:

  • (Aws::Record)

    builds and returns an instance of your model.

Raises:



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/aws-record/record/item_operations.rb', line 238

def find(opts)
  key = {}
  @keys.keys.each_value do |attr_sym|
    unless opts[attr_sym]
      raise Errors::KeyMissing.new(
        "Missing required key #{attr_sym} in #{opts}"
      )
    end
    attr_name = attributes.storage_name_for(attr_sym)
    key[attr_name] = attributes.attribute_for(attr_sym).
      serialize(opts[attr_sym])
  end
  request_opts = {
    table_name: table_name,
    key: key
  }
  resp = dynamodb_client.get_item(request_opts)
  if resp.item.nil?
    nil
  else
    build_item_from_resp(resp)
  end
end

#update(opts) ⇒ Object

Performs an Aws::DynamoDB::Client#update_item call immediately on the table, using the attribute key/value pairs provided.

Examples:

Usage Example

class MyModel
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name, range_key: true
  string_attr  :body
  boolean_attr :sir_not_appearing_in_this_example
end

MyModel.update(id: 1, name: "First", body: "Hello!")

Parameters:

  • opts (Hash)

    attribute-value pairs for the update operation you wish to perform. You must include all key attributes for a valid call, then you may optionally include any other attributes that you wish to update.

Raises:



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/aws-record/record/item_operations.rb', line 284

def update(opts)
  key = {}
  updates = {}
  @keys.keys.each_value do |attr_sym|
    unless value = opts.delete(attr_sym)
      raise Errors::KeyMissing.new(
        "Missing required key #{attr_sym} in #{opts}"
      )
    end
    attr_name = attributes.storage_name_for(attr_sym)
    key[attr_name] = attributes.attribute_for(attr_sym).serialize(value)
  end
  request_opts = {
    table_name: table_name,
    key: key
  }
  update_tuple = _build_update_expression(opts)
  unless update_tuple.nil?
    uex, exp_attr_names, exp_attr_values = update_tuple
    request_opts[:update_expression] = uex
    request_opts[:expression_attribute_names] = exp_attr_names
    request_opts[:expression_attribute_values] = exp_attr_values
  end
  dynamodb_client.update_item(request_opts)
end