Module: Dynamoid::Persistence::ClassMethods
- Defined in:
- lib/dynamoid/persistence.rb
Instance Method Summary collapse
-
#create_table(options = {}) ⇒ Object
Creates a table.
-
#delete_table ⇒ Object
Deletes the table for the model.
- #from_database(attrs = {}) ⇒ Object
-
#import(objects) ⇒ Object
Creates several models at once.
- #table_name ⇒ Object
Instance Method Details
#create_table(options = {}) ⇒ Object
Creates a table.
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( = {}) range_key_hash = if range_key { range_key => PrimaryKeyTypeMapping.dynamodb_type(attributes[range_key][:type], attributes[range_key]) } end = { 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() Dynamoid.adapter.create_table([:table_name], [:id], ) end |
#delete_table ⇒ Object
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
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. 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 |