Module: Dynamoid::Persistence::ClassMethods
- Defined in:
- lib/dynamoid/persistence.rb
Instance Method Summary collapse
-
#create(attrs = {}) ⇒ Dynamoid::Document
Create a model.
-
#create!(attrs = {}) ⇒ Dynamoid::Document
Create new model.
-
#create_table(options = {}) ⇒ Object
Create a table.
-
#delete_table ⇒ Object
Deletes the table for the model.
- #from_database(attrs = {}) ⇒ Object
-
#import(array_of_attributes) ⇒ Object
Create several models at once.
-
#inc(hash_key_value, range_key_value = nil, counters) ⇒ Dynamoid::Document/nil
Increase numeric field by specified value.
- #table_name ⇒ Object
-
#update(hash_key, range_key_value = nil, attrs) ⇒ Dynamoid::Doument
Update document with provided attributes.
-
#update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Dynamoid::Document|nil
Update document.
-
#upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Dynamoid::Document/nil
Update existing document or create new one.
Instance Method Details
#create(attrs = {}) ⇒ Dynamoid::Document
Create a model.
Initializes a new object and immediately saves it to the database. Validates model and runs callbacks: before_create, before_save, after_save and after_create. Accepts both Hash and Array of Hashes and can create several models.
103 104 105 106 107 108 109 |
# File 'lib/dynamoid/persistence.rb', line 103 def create(attrs = {}) if attrs.is_a?(Array) attrs.map { |attr| create(attr) } else build(attrs).tap(&:save) end end |
#create!(attrs = {}) ⇒ Dynamoid::Document
Create new model.
Initializes a new object and immediately saves it to the database. Raises an exception if validation failed. Accepts both Hash and Array of Hashes and can create several models.
122 123 124 125 126 127 128 |
# File 'lib/dynamoid/persistence.rb', line 122 def create!(attrs = {}) if attrs.is_a?(Array) attrs.map { |attr| create!(attr) } else build(attrs).tap(&:save!) end end |
#create_table(options = {}) ⇒ Object
Create a table.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dynamoid/persistence.rb', line 41 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, billing_mode: capacity_mode, 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() created_successfuly = Dynamoid.adapter.create_table([:table_name], [:id], ) if created_successfuly && self.[:expires] attribute = self.[:expires][:field] Dynamoid.adapter.update_time_to_live(table_name: table_name, attribute: attribute) end end |
#delete_table ⇒ Object
Deletes the table for the model
67 68 69 |
# File 'lib/dynamoid/persistence.rb', line 67 def delete_table Dynamoid.adapter.delete_table(table_name) end |
#from_database(attrs = {}) ⇒ Object
71 72 73 74 75 |
# File 'lib/dynamoid/persistence.rb', line 71 def from_database(attrs = {}) klass = choose_right_class(attrs) attrs_undumped = Undumping.undump_attributes(attrs, klass.attributes) klass.new(attrs_undumped).tap { |r| r.new_record = false } end |
#import(array_of_attributes) ⇒ Object
Create several models at once.
Neither callbacks nor validations run. It works efficiently because of using ‘BatchWriteItem` API call. Return array of models. Uses backoff specified by `Dynamoid::Config.backoff` config option
88 89 90 |
# File 'lib/dynamoid/persistence.rb', line 88 def import(array_of_attributes) Import.call(self, array_of_attributes) end |
#inc(hash_key_value, range_key_value = nil, counters) ⇒ Dynamoid::Document/nil
Increase numeric field by specified value.
Can update several fields at once. Uses efficient low-level ‘UpdateItem` API call.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/dynamoid/persistence.rb', line 234 def inc(hash_key_value, range_key_value = nil, counters) = if range_key value_casted = TypeCasting.cast_field(range_key_value, attributes[range_key]) value_dumped = Dumping.dump_field(value_casted, attributes[range_key]) { range_key: value_dumped } else {} end Dynamoid.adapter.update_item(table_name, hash_key_value, ) do |t| counters.each do |k, v| value_casted = TypeCasting.cast_field(v, attributes[k]) value_dumped = Dumping.dump_field(value_casted, attributes[k]) t.add(k => value_dumped) end end end |
#table_name ⇒ Object
25 26 27 28 29 |
# File 'lib/dynamoid/persistence.rb', line 25 def table_name table_base_name = [:name] || base_class.name.split('::').last.downcase.pluralize @table_name ||= [Dynamoid::Config.namespace.to_s, table_base_name].reject(&:empty?).join('_') end |
#update(hash_key, range_key_value = nil, attrs) ⇒ Dynamoid::Doument
Update document with provided attributes.
Instantiates document and saves changes. Runs validations and callbacks.
143 144 145 146 147 |
# File 'lib/dynamoid/persistence.rb', line 143 def update(hash_key, range_key_value = nil, attrs) model = find(hash_key, range_key: range_key_value, consistent_read: true) model.update_attributes(attrs) model end |
#update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Dynamoid::Document|nil
Update document.
Uses efficient low-level ‘UpdateItem` API call. Changes attibutes and loads new document version with one API call. Doesn’t run validations and callbacks. Can make conditional update. If a document doesn’t exist or specified conditions failed - returns ‘nil`
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/dynamoid/persistence.rb', line 168 def update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) optional_params = [range_key_value, attrs, conditions].compact if optional_params.first.is_a?(Hash) range_key_value = nil attrs, conditions = optional_params[0..1] else range_key_value = optional_params.first attrs, conditions = optional_params[1..2] end UpdateFields.call(self, partition_key: hash_key_value, sort_key: range_key_value, attributes: attrs, conditions: conditions) end |
#upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Dynamoid::Document/nil
Update existing document or create new one.
Similar to ‘.update_fields`. The only diffirence is - it creates new document in case the document doesn’t exist.
Uses efficient low-level ‘UpdateItem` API call. Changes attibutes and loads new document version with one API call. Doesn’t run validations and callbacks. Can make conditional update. If specified conditions failed - returns ‘nil`.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/dynamoid/persistence.rb', line 204 def upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) optional_params = [range_key_value, attrs, conditions].compact if optional_params.first.is_a?(Hash) range_key_value = nil attrs, conditions = optional_params[0..1] else range_key_value = optional_params.first attrs, conditions = optional_params[1..2] end Upsert.call(self, partition_key: hash_key_value, sort_key: range_key_value, attributes: attrs, conditions: conditions) end |