Module: Dynamoid::Persistence::ClassMethods

Defined in:
lib/dynamoid/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create_table(options = {}) ⇒ Object

Creates a table.

Parameters:

  • options (Hash) (defaults to: {})

    options to pass for table creation

Options Hash (options):

  • :id (Symbol)

    the id field for the table

  • :table_name (Symbol)

    the actual name for the table

  • :read_capacity (Integer)

    set the read capacity for the table; does not work on existing tables

  • :write_capacity (Integer)

    set the write capacity for the table; does not work on existing tables

  • {range_key (Hash)

    > :type} a hash of the name of the range key and a symbol of its type

  • :hash_key_type (Symbol)

    the dynamo type of the hash key (:string or :number)

Since:

  • 0.4.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dynamoid/persistence.rb', line 36

def create_table(options = {})
  range_key_hash = if range_key
                     { range_key => PrimaryKeyTypeMapping.dynamodb_type(attributes[range_key][:type], attributes[range_key]) }
                   end

  options = {
    id: hash_key,
    table_name: table_name,
    write_capacity: write_capacity,
    read_capacity: read_capacity,
    range_key: range_key_hash,
    hash_key_type: PrimaryKeyTypeMapping.dynamodb_type(attributes[hash_key][:type], attributes[hash_key]),
    local_secondary_indexes: local_secondary_indexes.values,
    global_secondary_indexes: global_secondary_indexes.values
  }.merge(options)

  Dynamoid.adapter.create_table(options[:table_name], options[:id], options)
end

#delete_tableObject

Deletes the table for the model



56
57
58
# File 'lib/dynamoid/persistence.rb', line 56

def delete_table
  Dynamoid.adapter.delete_table(table_name)
end

#from_database(attrs = {}) ⇒ Object



60
61
62
63
64
# File 'lib/dynamoid/persistence.rb', line 60

def from_database(attrs = {})
  clazz = choose_right_class(attrs)
  attrs_undumped = Undumping.undump_attributes(attrs, clazz.attributes)
  clazz.new(attrs_undumped).tap { |r| r.new_record = false }
end

#import(objects) ⇒ Object

Creates several models at once. Neither callbacks nor validations run. It works efficiently because of using BatchWriteItem.

Returns array of models

Uses backoff specified by ‘Dynamoid::Config.backoff` config option

Examples:

User.import([{ name: 'a' }, { name: 'b' }])

Parameters:

  • items (Array<Hash>)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/dynamoid/persistence.rb', line 78

def import(objects)
  documents = objects.map do |attrs|
    attrs = attrs.symbolize_keys

    if Dynamoid::Config.timestamps
      time_now = DateTime.now.in_time_zone(Time.zone)
      attrs[:created_at] ||= time_now
      attrs[:updated_at] ||= time_now
    end

    build(attrs).tap do |item|
      item.hash_key = SecureRandom.uuid if item.hash_key.blank?
    end
  end

  if Dynamoid.config.backoff
    backoff = nil

    array = documents.map do |d|
      Dumping.dump_attributes(d.attributes, attributes)
    end

    Dynamoid.adapter.batch_write_item(table_name, array) do |has_unprocessed_items|
      if has_unprocessed_items
        backoff ||= Dynamoid.config.build_backoff
        backoff.call
      else
        backoff = nil
      end
    end
  else
    array = documents.map do |d|
      Dumping.dump_attributes(d.attributes, attributes)
    end

    Dynamoid.adapter.batch_write_item(table_name, array)
  end

  documents.each { |d| d.new_record = false }
  documents
end

#table_nameObject



20
21
22
23
24
# File 'lib/dynamoid/persistence.rb', line 20

def table_name
  table_base_name = options[:name] || base_class.name.split('::').last.downcase.pluralize

  @table_name ||= [Dynamoid::Config.namespace.to_s, table_base_name].reject(&:empty?).join('_')
end