Class: DynaModel::Table

Inherits:
Object
  • Object
show all
Extended by:
AWS::DynamoDB::Types
Defined in:
lib/dyna_model/table.rb

Constant Summary collapse

RETURNED_CONSUMED_CAPACITY =
{
  none: "NONE",
  total: "TOTAL"
}
TYPE_INDICATOR =
{
  b: "B",
  n: "N",
  s: "S",
  ss: "SS",
  ns: "NS"
}
RETURN_VALUES =
{
  none: "NONE",
  all_old: "ALL_OLD",
  updated_old: "UPDATED_OLD",
  all_new: "ALL_NEW",
  updated_new: "UPDATED_NEW"
}
RETURN_VALUES_UPDATE_ONLY =
[
  :updated_old,
  :all_new,
  :updated_new
]
QUERY_SELECT =
{
  all: "ALL_ATTRIBUTES",
  projected: "ALL_PROJECTED_ATTRIBUTES",
  count: "COUNT",
  specific: "SPECIFIC_ATTRIBUTES"
}
COMPARISON_OPERATOR =
{
  eq: "EQ",
  le: "LE",
  lt: "LT",
  ge: "GE",
  gt: "GT",
  begins_with: "BEGINS_WITH",
  between: "BETWEEN",
  # Scan only
  ne: "NE",
  not_null: "NOT_NULL",
  null: "NULL",
  contains: "CONTAINS",
  not_contains: "NOT_CONTAINS",
  in: "IN"
}
CONDITIONAL_OPERATOR =
{
  and: "AND",
  or: "OR"
}
COMPARISON_OPERATOR_SCAN_ONLY =
[
  :ne, 
  :not_null,
  :null,
  :contains,
  :not_contains,
  :in
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Table

Returns a new instance of Table.



87
88
89
90
91
92
# File 'lib/dyna_model/table.rb', line 87

def initialize(model)
  @model = model
  @table_schema = model.table_schema
  self.load_schema
  self.validate_key_schema
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/dyna_model/table.rb', line 6

def client
  @client
end

#hash_keyObject (readonly)

Returns the value of attribute hash_key.



6
7
8
# File 'lib/dyna_model/table.rb', line 6

def hash_key
  @hash_key
end

#range_keysObject (readonly)

Returns the value of attribute range_keys.



6
7
8
# File 'lib/dyna_model/table.rb', line 6

def range_keys
  @range_keys
end

#schema_loaded_from_dynamoObject (readonly)

Returns the value of attribute schema_loaded_from_dynamo.



6
7
8
# File 'lib/dyna_model/table.rb', line 6

def schema_loaded_from_dynamo
  @schema_loaded_from_dynamo
end

#table_schemaObject (readonly)

Returns the value of attribute table_schema.



6
7
8
# File 'lib/dyna_model/table.rb', line 6

def table_schema
  @table_schema
end

Class Method Details

.attr_with_type(attr_name, value) ⇒ Object



83
84
85
# File 'lib/dyna_model/table.rb', line 83

def self.attr_with_type(attr_name, value)
  { attr_name => { TYPE_INDICATOR[type_from_value(value)] => value.to_s } }
end

.type_from_value(value) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/dyna_model/table.rb', line 73

def self.type_from_value(value)
  case
  when value.kind_of?(AWS::DynamoDB::Binary) then :b
  when value.respond_to?(:to_str) then :s
  when value.kind_of?(Numeric) then :n
  else
    raise ArgumentError, "unsupported attribute type #{value.class}"
  end
end

Instance Method Details

#batch_get_item(keys, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/dyna_model/table.rb', line 292

def batch_get_item(keys, options={})
  options[:return_consumed_capacity] ||= :none
  options[:select] ||= [] # no :projected option, always an array or :all
  options[:consistent_read] = false unless options[:consistent_read]

  raise ArgumentError, "must include between 1 - 100 keys" if keys.size == 0 || keys.size > 100
  keys_request = []
  keys.each do |k|
    key_request = {}
    if @primary_range_key
      hash_value, range_value = k.split(@model.guid_delimiter)
    else
      hash_value = k
    end
    key_request[@hash_key[:attribute_name]] = { @hash_key[:attribute_type] => hash_value.to_s }
    if @primary_range_key
      raise ArgumentError, "every key must include a range_value" if range_value.blank?
      key_request[@primary_range_key[:attribute_name]] = { @primary_range_key[:attribute_type] => range_value.to_s }
    end
    keys_request << key_request
  end

  request_items_request = {}
  request_items_request.merge!( keys: keys_request )
  if options[:select].blank?
    options[:select] = :all # for obj_from_attrs
  else
    request_items_request.merge!( attributes_to_get: [options[:select]].flatten )
  end
  request_items_request.merge!( consistent_read: options[:consistent_read] ) if options[:consistent_read]
  batch_get_item_request = {
    request_items: { @model.dynamo_db_table_name(options[:shard_name]) => request_items_request },
    return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]]
  }
  @model.dynamo_db_client.batch_get_item(batch_get_item_request)
end

#delete_item(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/dyna_model/table.rb', line 407

def delete_item(options={})
  raise ":delete_item => {...key_values...} required" unless options[:delete_item].present?
  options[:return_consumed_capacity] ||= :none
  options[:return_values] ||= :none
  raise ArgumentError, ":return_values must be one of (#{(RETURN_VALUES.keys - RETURN_VALUES_UPDATE_ONLY).join(", ")})" unless RETURN_VALUES[options[:return_values]] && !RETURN_VALUES_UPDATE_ONLY.include?(options[:return_values])
  key_request = {
    @hash_key[:attribute_name] => {
      @hash_key[:attribute_type] => options[:delete_item][:hash_value].to_s
    }
  }

  if @primary_range_key
    raise ArgumentError, "range_key was not provided to the delete_item command" if options[:delete_item][:range_value].blank?
    key_request.merge!({
      @primary_range_key[:attribute_name] => {
        @primary_range_key[:attribute_type] => options[:delete_item][:range_value].to_s
      }
    })
  end

  expected = {}
  conditional_operator = nil
  if options[:expected]
    raise ArgumentError, ":expected must be a hash" unless options[:expected].is_a?(Hash)
    options[:expected].each_pair do |k,v| 
      expected.merge!(self.attr_with_condition({ k => v}))
    end
    if options[:conditional_operator]
      raise ArgumentError, ":condition_operator invalid! Must be one of (#{CONDITIONAL_OPERATOR.keys.join(", ")})" unless CONDITIONAL_OPERATOR[options[:conditional_operator]]
      conditional_operator = CONDITIONAL_OPERATOR[options[:conditional_operator]]
    end
  end

  delete_item_request = {
    table_name: @model.dynamo_db_table_name(options[:shard_name]),
    key: key_request,
    return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]],
    return_values: RETURN_VALUES[options[:return_values]]
  }
  delete_item_request.merge!(expected: expected) unless expected.blank?
  delete_item_request.merge!(conditional_operator: conditional_operator) unless conditional_operator.blank? || expected.blank?
  @model.dynamo_db_client.delete_item(delete_item_request)
end

#get_item(hash_key, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dyna_model/table.rb', line 182

def get_item(hash_key, options={})
  options[:consistent_read] = false unless options[:consistent_read]
  options[:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL"
  options[:select] ||= [] # no :projected option, always an array or :all
  raise ArgumentError, "Invalid :select. GetItem :select must be an Array (blank for :all)" unless options[:select].is_a?(Array)

  get_item_request = {
    table_name: @model.dynamo_db_table_name(options[:shard_name]),
    key: hash_key_item_param(hash_key),
    consistent_read: options[:consistent_read],
    return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]]
  }
  if options[:select].blank?
    options[:select] = :all # for obj_from_attrs
  else
    get_item_request.merge!( attributes_to_get: [options[:select]].flatten )
  end
  @model.dynamo_db_client.get_item(get_item_request)
end

#hash_key_condition_param(hash_key, value) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/dyna_model/table.rb', line 172

def hash_key_condition_param(hash_key, value)
  hash_key_type = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == hash_key}[:attribute_type]
  {
    hash_key => {
      attribute_value_list: [hash_key_type => value.to_s],
      comparison_operator: COMPARISON_OPERATOR[:eq]
    }
  }
end

#hash_key_item_param(value) ⇒ Object



166
167
168
169
170
# File 'lib/dyna_model/table.rb', line 166

def hash_key_item_param(value)
  hash_key = @table_schema[:key_schema].find{|h| h[:key_type] == "HASH"}[:attribute_name]
  hash_key_type = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == hash_key}[:attribute_type]
  { hash_key => { hash_key_type => value.to_s } }
end

#load_schemaObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dyna_model/table.rb', line 94

def load_schema
  @schema_loaded_from_dynamo = @model.describe_table

  @schema_loaded_from_dynamo[:table][:key_schema].each do |key|
    key_attr = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == key[:attribute_name]}
    next if key_attr.nil?
    key_schema_attr = {
      attribute_name: key[:attribute_name],
      attribute_type: key_attr[:attribute_type]
    }

    if key[:key_type] == "HASH"
      @hash_key = key_schema_attr
    else
      (@range_keys ||= []) << key_schema_attr.merge(:primary_range_key => true)
      @primary_range_key = key_schema_attr.merge(:primary_range_key => true)
    end
  end

  if @schema_loaded_from_dynamo[:table][:local_secondary_indexes] || @schema_loaded_from_dynamo[:table][:global_secondary_indexes]
    ((@schema_loaded_from_dynamo[:table][:local_secondary_indexes] || []) + (@schema_loaded_from_dynamo[:table][:global_secondary_indexes] || [])).each do |key|
      si_range_key = key[:key_schema].find{|h| h[:key_type] == "RANGE" }
      next if si_range_key.nil?
      si_range_attribute = @table_schema[:attribute_definitions].find{|h| h[:attribute_name] == si_range_key[:attribute_name]}
      next if si_range_attribute.nil?
      (@range_keys ||= []) << {
        attribute_name: si_range_key[:attribute_name],
        attribute_type: si_range_attribute[:attribute_type],
        index_name: key[:index_name]
      }
    end
  end

  @schema_loaded_from_dynamo
end

#query(hash_value, options = {}) ⇒ Object

options

* consistent_read
* return_consumed_capacity
* order
* select
* range


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/dyna_model/table.rb', line 208

def query(hash_value, options={})
  options[:consistent_read] = false unless options[:consistent_read]
  options[:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL"
  options[:order] ||= :desc
  #options[:index_name] ||= :none
  #AWS::DynamoDB::Errors::ValidationException: ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName
  #options[:limit] ||= 10
  #options[:exclusive_start_key]
  #options[:query_filter]

  key_conditions = {}
  gsi = nil
  if options[:global_secondary_index]
    gsi = @table_schema[:global_secondary_indexes].select{ |gsi| gsi[:index_name].to_s == options[:global_secondary_index].to_s}.first
    raise ArgumentError, "Could not find Global Secondary Index '#{options[:global_secondary_index]}'" unless gsi
    gsi_hash_key = gsi[:key_schema].find{|h| h[:key_type] == "HASH"}[:attribute_name]
    key_conditions.merge!(hash_key_condition_param(gsi_hash_key, hash_value))
  else
    hash_key = @table_schema[:key_schema].find{|h| h[:key_type] == "HASH"}[:attribute_name]
    key_conditions.merge!(hash_key_condition_param(hash_key, hash_value))
  end

  query_request = {
    table_name: @model.dynamo_db_table_name(options[:shard_name]),
    key_conditions: key_conditions,
    consistent_read: options[:consistent_read],
    return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]],
    scan_index_forward: (options[:order] == :asc)
  }

  if options[:range] 
    raise ArgumentError, "Table does not use a range key in its schema!" if @range_keys.blank?
    attr_with_condition_hash = self.attr_with_condition(options[:range])
    range_key = @range_keys.find{|k| k[:attribute_name] == attr_with_condition_hash.keys.first}
    raise ArgumentError, ":range key must be a valid Range attribute" unless range_key

    if range_key.has_key?(:index_name) # Local/Global Secondary Index
      options[:index_name] = range_key[:index_name]
      query_request[:index_name] = range_key[:index_name]
    end

    key_conditions.merge!(attr_with_condition_hash)
  end

  query_filter = {}
  conditional_operator = nil
  if options[:query_filter]
    raise ArgumentError, ":query_filter must be a hash" unless options[:query_filter].is_a?(Hash)
    options[:query_filter].each_pair do |k,v| 
      query_filter.merge!(self.attr_with_condition({ k => v}))
    end
    if options[:conditional_operator]
      raise ArgumentError, ":condition_operator invalid! Must be one of (#{CONDITIONAL_OPERATOR.keys.join(", ")})" unless CONDITIONAL_OPERATOR[options[:conditional_operator]]
      conditional_operator = CONDITIONAL_OPERATOR[options[:conditional_operator]]
    end
  end
  query_request.merge!(query_filter: query_filter) unless query_filter.blank?
  query_request.merge!(conditional_operator: conditional_operator) unless conditional_operator.blank? || query_filter.blank?

  if options[:global_secondary_index] # Override index_name if using GSI
    # You can only select projected attributes from a GSI
    options[:select] = :projected #if options[:select].blank?
    options[:index_name] = gsi[:index_name]
    query_request.merge!(index_name: gsi[:index_name])
  end
  options[:select] ||= :all # :all, :projected, :count, []
  if options[:select].is_a?(Array)
    attrs_to_select = [options[:select].map(&:to_s)].flatten
    attrs_to_select << @hash_key[:attribute_name]
    attrs_to_select << @primary_range_key[:attribute_name] if @primary_range_key
    query_request.merge!({
      select: QUERY_SELECT[:specific],
      attributes_to_get: attrs_to_select.uniq
    })
  else
    query_request.merge!({ select: QUERY_SELECT[options[:select]] })
  end
  
  query_request.merge!({ limit: options[:limit].to_i }) if options.has_key?(:limit)
  query_request.merge!({ exclusive_start_key: options[:exclusive_start_key] }) if options[:exclusive_start_key]

  @model.dynamo_db_client.query(query_request)
end

#scan(options = {}) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/dyna_model/table.rb', line 453

def scan(options={})
  options[:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL"
  # Default if not already set
  options[:select] ||= :all # :all, :projected, :count, []

  scan_request = {
    table_name: @model.dynamo_db_table_name(options[:shard_name]),
    return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]]
  }

  scan_request.merge!({ limit: options[:limit].to_i }) if options.has_key?(:limit)
  scan_request.merge!({ exclusive_start_key: options[:exclusive_start_key] }) if options[:exclusive_start_key]

  if options[:select].is_a?(Array)
    attrs_to_select = [options[:select].map(&:to_s)].flatten
    attrs_to_select << @hash_key[:attribute_name]
    attrs_to_select << @primary_range_key[:attribute_name] if @primary_range_key
    scan_request.merge!({
      select: QUERY_SELECT[:specific],
      attributes_to_get: attrs_to_select.uniq
    })
  else
    scan_request.merge!({ select: QUERY_SELECT[options[:select]] })
  end

  # :scan_filter => { :name.begins_with => "a" }
  scan_filter = {}
  conditional_operator = nil
  if options[:scan_filter].present?
    options[:scan_filter].each_pair.each do |k,v|
      scan_filter.merge!(self.attr_with_condition({ k => v}))
    end
  end
  if options[:conditional_operator]
    raise ArgumentError, ":condition_operator invalid! Must be one of (#{CONDITIONAL_OPERATOR.keys.join(", ")})" unless CONDITIONAL_OPERATOR[options[:conditional_operator]]
    conditional_operator = CONDITIONAL_OPERATOR[options[:conditional_operator]]
  end
  scan_request.merge!(scan_filter: scan_filter) unless scan_filter.blank?
  scan_request.merge!(conditional_operator: conditional_operator) unless conditional_operator.blank? || scan_filter.blank?
  scan_request.merge!(segment: options[:segment].to_i) if options[:segment].present?
  scan_request.merge!(total_segments: options[:total_segments].to_i) if options[:total_segments].present?

  @model.dynamo_db_client.scan(scan_request)
end

#validate_key_schemaObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dyna_model/table.rb', line 130

def validate_key_schema
  if @schema_loaded_from_dynamo[:table][:key_schema].sort_by { |k| k[:key_type] } != @table_schema[:key_schema].sort_by { |k| k[:key_type] }
    raise ArgumentError, "It appears your key schema (Hash Key/Range Key) have changed from the table definition. Rebuilding the table is necessary."
  end

  if @schema_loaded_from_dynamo[:table][:attribute_definitions].sort_by { |k| k[:attribute_name] } != @table_schema[:attribute_definitions].sort_by { |k| k[:attribute_name] }
    raise ArgumentError, "It appears your attribute definition (types?) have changed from the table definition. Rebuilding the table is necessary."
  end

  index_keys_to_reject = [:index_status, :index_size_bytes, :item_count]

  if @schema_loaded_from_dynamo[:table][:local_secondary_indexes].blank? != @table_schema[:local_secondary_indexes].blank?
    raise ArgumentError, "It appears your local secondary indexes have changed from the table definition. Rebuilding the table is necessary."
  end
  
  if @schema_loaded_from_dynamo[:table][:local_secondary_indexes] && (@schema_loaded_from_dynamo[:table][:local_secondary_indexes].dup.collect {|i| i.delete_if{|k, v| index_keys_to_reject.include?(k) }; i }.sort_by { |lsi| lsi[:index_name] } != @table_schema[:local_secondary_indexes].sort_by { |lsi| lsi[:index_name] })
    raise ArgumentError, "It appears your local secondary indexes have changed from the table definition. Rebuilding the table is necessary."
  end

  if @schema_loaded_from_dynamo[:table][:global_secondary_indexes].blank? != @table_schema[:global_secondary_indexes].blank?
    raise ArgumentError, "It appears your global secondary indexes have changed from the table definition. Rebuilding the table is necessary."
  end

  if @schema_loaded_from_dynamo[:table][:global_secondary_indexes] && (@schema_loaded_from_dynamo[:table][:global_secondary_indexes].dup.collect {|i| i.delete_if{|k, v| index_keys_to_reject.include?(k) }; i }.sort_by { |gsi| gsi[:index_name] } != @table_schema[:global_secondary_indexes].sort_by { |gsi| gsi[:index_name] })
    raise ArgumentError, "It appears your global secondary indexes have changed from the table definition. Rebuilding the table is necessary."
  end

  if @schema_loaded_from_dynamo[:table][:provisioned_throughput][:read_capacity_units] != @table_schema[:provisioned_throughput][:read_capacity_units]
    DynaModel::Config.logger.error "read_capacity_units mismatch. Need to update table?"
  end

  if @schema_loaded_from_dynamo[:table][:provisioned_throughput][:write_capacity_units] != @table_schema[:provisioned_throughput][:write_capacity_units]
    DynaModel::Config.logger.error "write_capacity_units mismatch. Need to update table?"
  end
end

#write(attributes, options = {}) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/dyna_model/table.rb', line 329

def write(attributes, options={})
  options[:return_consumed_capacity] ||= :none
  options[:return_values] ||= :none
  options[:update_item] = false unless options[:update_item]

  expected = {}
  conditional_operator = nil
  if options[:expected]
    raise ArgumentError, ":expected must be a hash" unless options[:expected].is_a?(Hash)
    options[:expected].each_pair do |k,v| 
      expected.merge!(self.attr_with_condition({ k => v}))
    end
    if options[:conditional_operator]
      raise ArgumentError, ":condition_operator invalid! Must be one of (#{CONDITIONAL_OPERATOR.keys.join(", ")})" unless CONDITIONAL_OPERATOR[options[:conditional_operator]]
      conditional_operator = CONDITIONAL_OPERATOR[options[:conditional_operator]]
    end
  end

  if options[:update_item]
    # UpdateItem
    key_request = {
      @hash_key[:attribute_name] => {
        @hash_key[:attribute_type] => options[:update_item][:hash_value].to_s,
      }
    }
    if @primary_range_key
      raise ArgumentError, "range_key was not provided to the write command" if options[:update_item][:range_value].blank?
      key_request.merge!({
        @primary_range_key[:attribute_name] => {
          @primary_range_key[:attribute_type] => options[:update_item][:range_value].to_s
        }
      })
    end
    attrs_to_update = {}
    attributes.each_pair do |k,v|
      next if k == @hash_key[:attribute_name] || (@primary_range_key && k == @primary_range_key[:attribute_name])
      if v.nil?
        attrs_to_update.merge!({ k => { :action => "DELETE" } })
      else
        attrs_to_update.merge!({
          k => {
            value: self.class.attr_with_type(k,v).values.last,
            action: "PUT"
          }
        })
      end
    end
    raise ArgumentError, ":return_values must be one of (#{RETURN_VALUES.keys.join(", ")})" unless RETURN_VALUES[options[:return_values]]
    update_item_request = {
      table_name: @model.dynamo_db_table_name(options[:shard_name]),
      key: key_request,
      attribute_updates: attrs_to_update,
      return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]],
      return_values: RETURN_VALUES[options[:return_values]]
    }
    update_item_request.merge!(expected: expected) unless expected.blank?
    update_item_request.merge!(conditional_operator: conditional_operator) unless conditional_operator.blank? || expected.blank?
    @model.dynamo_db_client.update_item(update_item_request)
  else
    # PutItem
    items = {}
    attributes.each_pair do |k,v|
      next if v.blank? # If empty string or nil, skip...
      items.merge!(self.class.attr_with_type(k,v))
    end
    raise ArgumentError, ":return_values must be one of (#{(RETURN_VALUES.keys - RETURN_VALUES_UPDATE_ONLY).join(", ")})" unless RETURN_VALUES[options[:return_values]] && !RETURN_VALUES_UPDATE_ONLY.include?(options[:return_values])
    put_item_request = {
      table_name: @model.dynamo_db_table_name(options[:shard_name]),
      item: items,
      return_consumed_capacity: RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]],
      return_values: RETURN_VALUES[options[:return_values]]
    }
    put_item_request.merge!(expected: expected) unless expected.blank?
    put_item_request.merge!(conditional_operator: conditional_operator) unless conditional_operator.blank? || expected.blank?
    @model.dynamo_db_client.put_item(put_item_request)
  end
end