Class: Lotus::Model::Adapters::Dynamodb::Collection Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • client (AWS::DynamoDB::Client)

    DynamoDB client

  • coercer (Lotus::Model::Adapters::Dynamodb::Coercer)
  • name (Symbol)

    the name of the collection (eg. ‘:users`)

  • identity (Symbol)

    the primary key of the collection (eg. ‘:id`).

Since:

  • 0.1.0



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

#identityObject (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.

Since:

  • 0.1.0



40
41
42
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 40

def identity
  @identity
end

#nameObject (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.

Since:

  • 0.1.0



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.

Parameters:

  • entity (Object)

    the entity to persist

Returns:

  • the primary key of the just created record.

See Also:

Since:

  • 0.1.0



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.

Parameters:

  • item (Hash)

    the serialized item

Returns:

  • (Hash)

    the deserialized record

See Also:

  • AWS::DynamoDB::Types

Since:

  • 0.1.0



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.

Parameters:

  • response (Hash)

    the serialized response

  • previous_response (Response) (defaults to: nil)

    deserialized response from a previous operation

Returns:

  • (Response)

    the deserialized response

Since:

  • 0.1.0



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.

Parameters:

  • column (String)

    the attribute column

  • value (Object)

    the attribute value

Returns:

  • (Hash)

    the formatted attribute

See Also:

  • AWS::DynamoDB::Types

Since:

  • 0.1.0



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.

Parameters:

  • key (Array)

    the identity of the object

Returns:

  • (Hash)

    the serialized record

See Also:

Since:

  • 0.1.0



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.

Parameters:

  • column (String)

    column to check

  • index (String) (defaults to: nil)

    index to check (defaults to table itself)

Returns:

  • (Boolean)

See Also:

Since:

  • 0.1.0



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.

Parameters:

  • index (String) (defaults to: nil)

    index to check (defaults to table itself)

Returns:

  • (Hash)

    key schema definition

See Also:

Since:

  • 0.1.0



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.

Parameters:

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

    AWS::DynamoDB::Client options

  • previous_response (Response) (defaults to: nil)

    deserialized response from a previous operation

Returns:

  • (Array<Hash>)

    the serialized entities

See Also:

Since:

  • 0.1.0



148
149
150
151
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 148

def query(options = {}, previous_response = nil)
  response = @client.query(options.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.

Parameters:

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

    AWS::DynamoDB::Client options

  • previous_response (Response) (defaults to: nil)

    deserialized response from a previous operation

Returns:

  • (Array<Hash>)

    the serialized entities

See Also:

Since:

  • 0.1.0



164
165
166
167
# File 'lib/lotus/model/adapters/dynamodb/collection.rb', line 164

def scan(options = {}, previous_response = nil)
  response = @client.scan(options.merge(table_name: name))
  deserialize_response(response, previous_response)
end

#schemaHash

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.

Parameters:

  • entity (Hash)

    the entity

Returns:

  • (Hash)

    the serialized attributes

See Also:

  • AWS::DynamoDB::Types

Since:

  • 0.1.0



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.

Parameters:

  • record (Hash)

    the serialized record

Returns:

  • (Hash)

    the serialized item

See Also:

  • AWS::DynamoDB::Types

Since:

  • 0.1.0



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.

Parameters:

  • record (Hash, Array)

    the serialized record or primary key

Returns:

  • (Hash)

    the serialized key

See Also:

  • AWS::DynamoDB::Types

Since:

  • 0.1.0



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