Class: Lotus::Model::Adapters::Dynamodb::Collection Private
- Inherits:
-
Object
- Object
- Lotus::Model::Adapters::Dynamodb::Collection
- Includes:
- AWS::DynamoDB::Types
- Defined in:
- lib/lotus/model/adapters/dynamodb/collection.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Acts like table, using AWS::DynamoDB::Client.
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
- #identity ⇒ Object readonly private
- #name ⇒ Object readonly private
Instance Method Summary collapse
-
#create(entity) ⇒ Object
private
Creates a record for the given entity and returns a primary key.
-
#delete(entity) ⇒ Object
private
Deletes the record corresponding to the given entity.
-
#deserialize_item(record) ⇒ Hash
private
Deserialize item from DynamoDB response.
-
#deserialize_response(response, previous_response = nil) ⇒ Response
private
Deserialize DynamoDB scan/query response.
-
#format_attribute(column, value) ⇒ Hash
private
Coerce and format attribute value to match DynamoDB type.
-
#get(key) ⇒ Hash
private
Returns an unique record from the given collection, with the given id.
-
#initialize(client, coercer, name, identity) ⇒ Collection
constructor
private
Initialize a collection.
-
#key?(column, index = nil) ⇒ Boolean
private
Checks if given column is in key schema or not.
-
#key_schema(index = nil) ⇒ Hash
private
Maps table key schema to hash with attribute name as key and key type as value.
-
#query(options = {}, previous_response = nil) ⇒ Array<Hash>
private
Performs DynamoDB query operation.
-
#scan(options = {}, previous_response = nil) ⇒ Array<Hash>
private
Performs DynamoDB scan operation.
-
#schema ⇒ Hash
private
Fetches DynamoDB table schema.
-
#serialize_attributes(entity) ⇒ Hash
private
Serialize given entity to exclude key schema attributes.
-
#serialize_item(record) ⇒ Hash
private
Serialize given record to have proper attributes for ‘item’ query.
-
#serialize_key(record) ⇒ Hash
private
Serialize given record or primary key to have proper attributes for ‘key’ query.
-
#update(entity) ⇒ Object
private
Updates the record corresponding to the given entity.
Constructor Details
#initialize(client, coercer, name, identity) ⇒ Collection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a collection.
52 53 54 55 56 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 52 def initialize(client, coercer, name, identity) @client, @coercer = client, coercer @name, @identity = name.to_s, identity @key_schema = {} end |
Instance Attribute Details
#identity ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
40 41 42 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 40 def identity @identity end |
#name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
33 34 35 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 33 def name @name end |
Instance Method Details
#create(entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a record for the given entity and returns a primary key.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 69 def create(entity) entity[identity] ||= SecureRandom.uuid @client.put_item( table_name: name, item: serialize_item(entity), ) entity[identity] end |
#delete(entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes the record corresponding to the given entity.
106 107 108 109 110 111 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 106 def delete(entity) @client.delete_item( table_name: name, key: serialize_key(entity), ) end |
#deserialize_item(record) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deserialize item from DynamoDB response.
320 321 322 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 320 def deserialize_item(record) Lotus::Utils::Hash.new(values_from_response_hash(record)).symbolize! end |
#deserialize_response(response, previous_response = nil) ⇒ Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deserialize DynamoDB scan/query response.
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 298 def deserialize_response(response, previous_response = nil) current_response = previous_response || Response.new current_response.count += response[:count] current_response.entities += response[:member].map do |item| deserialize_item(item) end if response[:member] current_response.last_evaluated_key = response[:last_evaluated_key] current_response end |
#format_attribute(column, value) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Coerce and format attribute value to match DynamoDB type.
235 236 237 238 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 235 def format_attribute(column, value) value = @coercer.public_send(:"serialize_#{ column }", value) format_attribute_value(value) end |
#get(key) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an unique record from the given collection, with the given id.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 125 def get(key) return if key.any? { |v| v.to_s == "" } return if key.count != key_schema.count response = @client.get_item( table_name: name, key: serialize_key(key), ) deserialize_item(response[:item]) if response[:item] end |
#key?(column, index = nil) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if given column is in key schema or not.
220 221 222 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 220 def key?(column, index = nil) key_schema(index).has_key?(column) end |
#key_schema(index = nil) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Maps table key schema to hash with attribute name as key and key type as value.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 192 def key_schema(index = nil) return @key_schema[index] if @key_schema[index] current_schema = if index everything = Array(schema[:local_secondary_indexes]) + Array(schema[:global_secondary_indexes]) indexes = Hash[everything.map { |i| [i[:index_name], i] }] indexes[index][:key_schema] else schema[:key_schema] end @key_schema[index] ||= Hash[current_schema.to_a.map do |key| [key[:attribute_name].to_sym, key[:key_type]] end] end |
#query(options = {}, previous_response = nil) ⇒ Array<Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Performs DynamoDB query operation.
148 149 150 151 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 148 def query( = {}, previous_response = nil) response = @client.query(.merge(table_name: name)) deserialize_response(response, previous_response) end |
#scan(options = {}, previous_response = nil) ⇒ Array<Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Performs DynamoDB scan operation.
164 165 166 167 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 164 def scan( = {}, previous_response = nil) response = @client.scan(.merge(table_name: name)) deserialize_response(response, previous_response) end |
#schema ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetches DynamoDB table schema.
177 178 179 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 177 def schema @schema ||= @client.describe_table(table_name: name).fetch(:table) end |
#serialize_attributes(entity) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serialize given entity to exclude key schema attributes.
282 283 284 285 286 287 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 282 def serialize_attributes(entity) keys = key_schema.keys Hash[entity.reject { |k, _| keys.include?(k) }.map do |k, v| [k.to_s, { value: format_attribute_value(v), action: "PUT" }] end] end |
#serialize_item(record) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serialize given record to have proper attributes for ‘item’ query.
250 251 252 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 250 def serialize_item(record) Hash[record.map { |k, v| [k.to_s, format_attribute_value(v)] }] end |
#serialize_key(record) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serialize given record or primary key to have proper attributes for ‘key’ query.
265 266 267 268 269 270 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 265 def serialize_key(record) Hash[key_schema.keys.each_with_index.map do |k, idx| v = record.is_a?(Hash) ? record[k] : record[idx] [k.to_s, format_attribute(k, v)] end] end |
#update(entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Updates the record corresponding to the given entity.
89 90 91 92 93 94 95 |
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 89 def update(entity) @client.update_item( table_name: name, key: serialize_key(entity), attribute_updates: serialize_attributes(entity), ) end |