Module: MinceDynamoDb::Interface

Defined in:
lib/mince_dynamo_db/interface.rb

Class Method Summary collapse

Class Method Details

.add(collection_name, hash) ⇒ Object

Inserts one record into a collection.

Parameters:

  • collection_name (String)

    the name of the collection

  • hash (Hash)

    a hash of data to be added to the collection


53
54
55
56
# File 'lib/mince_dynamo_db/interface.rb', line 53

def self.add(collection_name, hash)
  hash.delete_if{|k,v| v.nil? }
  items(collection_name).create(sanitized_hash(hash))
end

.array_contains(collection_name, key, value) ⇒ Array

Returns all records where the given key contains the given value

Parameters:

  • collection_name (String)

    the name of the collection

  • key (String)

    the key to find records by

  • value (*)

    the value to find a record by

Returns:

  • (Array)

    all records where the key contains the given value


181
182
183
# File 'lib/mince_dynamo_db/interface.rb', line 181

def self.array_contains(collection_name, key, value)
  array_to_hash items(collection_name).where(key.to_sym).contains(value).select
end

.array_to_hash(array) ⇒ Object

Takes an array of DynamoDB items and returns the attributes of each item as a hash. calls


239
240
241
# File 'lib/mince_dynamo_db/interface.rb', line 239

def self.array_to_hash(array)
  array.map{|a| to_hash(a) }
end

.clearObject

Clears the data store. Mainly used for rolling back the data store in tests.


192
193
194
195
196
197
198
199
# File 'lib/mince_dynamo_db/interface.rb', line 192

def self.clear
  db.tables.each do |t|
    t.hash_key unless t.schema_loaded? # to load the schema
    t.items.each do |i|
      i.delete
    end
  end
end

.collection(collection_name) ⇒ AWS::DynamoDB::Table

Returns the collection, or table, for a given collection name

The schema must be loaded before any queries are made against a collection There are a couple ways to do this, from digging in their documentation about this topic. One is to have the schema loaded in memory, which is not recommended for production code, the other way is to call hash_key :) not sure why this works.

Parameters:

  • collection_name (String)

    the name of the collection

Returns:

  • (AWS::DynamoDB::Table)

    the AWS::DynamoDB::Table for the given collection_name


220
221
222
# File 'lib/mince_dynamo_db/interface.rb', line 220

def self.collection(collection_name)
  DataStore.collection(collection_name)
end

.collection_status(collection_name) ⇒ Object


26
27
28
# File 'lib/mince_dynamo_db/interface.rb', line 26

def self.collection_status(collection_name)
  collection(collection_name).status
end

.collectionsObject


248
249
250
# File 'lib/mince_dynamo_db/interface.rb', line 248

def self.collections
  DataStore.collections
end

.containing_any(collection_name, key, values) ⇒ Array

Returns all records where the given key contains any of the values provided

Parameters:

  • collection_name (String)

    the name of the collection

  • key (String)

    the key to find the record by

  • values (Array)

    an array of values that the record could contain

Returns:

  • (Array)

    all records that contain any of the values given


171
172
173
# File 'lib/mince_dynamo_db/interface.rb', line 171

def self.containing_any(collection_name, key, values)
  array_to_hash items(collection_name).where(key.to_sym).in(values).select
end

.create_collection(collection_name, read_rate = 10, write_rate = 5) ⇒ Object


22
23
24
# File 'lib/mince_dynamo_db/interface.rb', line 22

def self.create_collection(collection_name, read_rate=10, write_rate=5)
  collections.create(collection_name, read_rate, write_rate)
end

.dbObject

Returns the database object which comes from MinceDynamoDb::Connection


244
245
246
# File 'lib/mince_dynamo_db/interface.rb', line 244

def self.db
  DataStore.db
end

.delete_by_params(collection_name, params) ⇒ Object

Deletes all records that matche the given criteria from the data store.


186
187
188
# File 'lib/mince_dynamo_db/interface.rb', line 186

def self.delete_by_params(collection_name, params)
  get_items_by_params(collection_name, params).each(&:delete)
end

.delete_collection(collection_name) ⇒ Object


14
15
16
# File 'lib/mince_dynamo_db/interface.rb', line 14

def self.delete_collection(collection_name)
  raise %(The method `MinceDynamoDb::DataStore.singleton.delete_collection` is not implemented, you should implement it for us!)
end

.delete_field(collection_name, field) ⇒ Object

Not yet implemented


10
11
12
# File 'lib/mince_dynamo_db/interface.rb', line 10

def self.delete_field(collection_name, field)
  raise %(The method `MinceDynamoDb::DataStore.singleton.delete_field` is not implemented, you should implement it for us!)
end

.find(collection_name, key, value) ⇒ Hash

Gets a hash of attributes for a record for the given value for the collection's primary key

Parameters:

  • collection_name (String)

    the name of the collection

  • key (String)

    the key to find a record by (not used)

  • value (*)

    a value the find a record by

Returns:

  • (Hash)

    a record that matches the given key and value using the collection's primary key


127
128
129
# File 'lib/mince_dynamo_db/interface.rb', line 127

def self.find(collection_name, key, value)
  find_item(collection_name, value).to_h
end

.find_all(collection_name) ⇒ Array

Gets all records for a collection

Parameters:

  • collection_name (String)

    the name of the collection

Returns:

  • (Array)

    all records for the given collection name


117
118
119
# File 'lib/mince_dynamo_db/interface.rb', line 117

def self.find_all(collection_name)
  array_to_hash(items(collection_name).select)
end

.find_item(collection_name, value) ⇒ AWS::DynamoDB::AttributeCollection

Gets a record for the given value for the collection's primary key

Parameters:

  • collection_name (String)

    the name of the collection

  • value (*)

    a value the find a record by

Returns:

  • (AWS::DynamoDB::AttributeCollection)

    a record that matches the given key and value using the collection's primary key


137
138
139
# File 'lib/mince_dynamo_db/interface.rb', line 137

def self.find_item(collection_name, value)
  items(collection_name).at(value).attributes
end

.generate_unique_id(salt) ⇒ String

Note:

This is necessary because different databases use different types of primary key values. Thus, each mince implementation must define how to generate a unique id.

Generates a unique ID for a database record

Parameters:

  • salt (#to_s)

    any object that responds to #to_s

Returns:

  • (String)

    A unique id based on the salt and the current time


45
46
47
# File 'lib/mince_dynamo_db/interface.rb', line 45

def self.generate_unique_id(salt)
  Digest::SHA256.hexdigest("#{Time.current.utc}#{salt}")[0..6]
end

.get_all_for_key_with_value(collection_name, key, value) ⇒ Array

Gets all records that have the value for a given key.

Parameters:

  • collection_name (String)

    the name of the collection

  • key (String)

    the key, or field, to get a record for

  • value (*)

    the value to get a record for

Returns:

  • (Array)

    an array of records that match the key and value


85
86
87
# File 'lib/mince_dynamo_db/interface.rb', line 85

def self.get_all_for_key_with_value(collection_name, key, value)
  get_by_params(collection_name, key => value)
end

.get_by_params(collection_name, hash) ⇒ Array

Gets all records that have all of the keys and values in the given hash.

Parameters:

  • collection_name (String)

    the name of the collection

  • hash (Hash)

    a hash to get a record for

Returns:

  • (Array)

    an array of all records matching the given hash for the collection


104
105
106
# File 'lib/mince_dynamo_db/interface.rb', line 104

def self.get_by_params(collection_name, hash)
  array_to_hash(get_items_by_params(collection_name, hash).select)
end

.get_for_key_with_value(collection_name, key, value) ⇒ Hash

Gets the first record that has the value for a given key.

Parameters:

  • collection_name (String)

    the name of the collection

  • key (String)

    the key to find a record by

  • value (String)

    the value to find a record by

Returns:

  • (Hash)

    a hash for the record found by the key and value in the collection


95
96
97
# File 'lib/mince_dynamo_db/interface.rb', line 95

def self.get_for_key_with_value(collection_name, key, value)
  get_all_for_key_with_value(collection_name, key.to_s, value).first
end

.get_items_by_params(collection_name, hash) ⇒ Object


108
109
110
111
# File 'lib/mince_dynamo_db/interface.rb', line 108

def self.get_items_by_params(collection_name, hash)
  hash = HashWithIndifferentAccess.new(hash)
  items(collection_name).where(hash)
end

.increment_field_by_amount(collection_name, id, field_name, amount) ⇒ Object


18
19
20
# File 'lib/mince_dynamo_db/interface.rb', line 18

def self.increment_field_by_amount(collection_name, id, field_name, amount)
  raise %(The method `MinceDynamoDb::DataStore.singleton.increment_field_by_amount` is not implemented, you should implement it for us!)
end

.items(collection_name) ⇒ Object


252
253
254
# File 'lib/mince_dynamo_db/interface.rb', line 252

def self.items(collection_name)
  DataStore.items(collection_name)
end

.primary_keyString

Returns the primary key identifier for records. This is necessary because not all databases use the same primary key.

Returns:

  • (String)

    the name of the primary key field.


34
35
36
# File 'lib/mince_dynamo_db/interface.rb', line 34

def self.primary_key
  Config.primary_key
end

.push_to_array(collection_name, identifying_key, identifying_value, array_key, value_to_push) ⇒ Object

Pushes a value to a record's key that is an array

Parameters:

  • collection_name (String)

    the name of the collection

  • identifying_key (String)

    the field used to find the record

  • identifying_value (*)

    the value used to find the record

  • array_key (String)

    the field to push an array to

  • value_to_push (*)

    the value to push to the array


148
149
150
151
# File 'lib/mince_dynamo_db/interface.rb', line 148

def self.push_to_array(collection_name, identifying_key, identifying_value, array_key, value_to_push)
  item = items(collection_name).where(identifying_key.to_s => identifying_value).first
  item.attributes.add(array_key => [sanitized_field(value_to_push)])
end

.remove_from_array(collection_name, identifying_key, identifying_value, array_key, value_to_remove) ⇒ Object

Removes a value from a record's key that is an array

Parameters:

  • collection_name (String)

    the name of the collection

  • identifying_key (String)

    the field used to find the record

  • identifying_value (*)

    the value used to find the record

  • array_key (String)

    the field to push an array from

  • value_to_remove (*)

    the value to remove from the array


160
161
162
163
# File 'lib/mince_dynamo_db/interface.rb', line 160

def self.remove_from_array(collection_name, identifying_key, identifying_value, array_key, value_to_remove)
  item = items(collection_name).where(identifying_key.to_s => identifying_value).select.first      
  item.attributes.delete(array_key => [value_to_remove])
end

.replace(collection_name, hash) ⇒ Object

Replaces a record in the collection based on the primary key's value. The hash must contain a key, defined by the primary_key_identifier method, with a value. If a record in the data store is found with that key and value, the entire record will be replaced with the given hash.

Parameters:

  • collection_name (String)

    the name of the collection

  • hash (Hash)

    a hash to replace the record in the collection with


64
65
66
# File 'lib/mince_dynamo_db/interface.rb', line 64

def self.replace(collection_name, hash)
  items(collection_name).put(sanitized_hash(hash))
end

.sanitized_field(field) ⇒ Object


224
225
226
# File 'lib/mince_dynamo_db/interface.rb', line 224

def self.sanitized_field(field)
  DataSanitizer.prepare_field_for_storage(field)
end

.sanitized_hash(hash) ⇒ Object


228
229
230
# File 'lib/mince_dynamo_db/interface.rb', line 228

def self.sanitized_hash(hash)
  DataSanitizer.prepare_hash_for_storage(hash)
end

.set_data(data) ⇒ Object


201
202
203
204
205
206
207
208
209
# File 'lib/mince_dynamo_db/interface.rb', line 201

def self.set_data(data)
  clear

  data.each do |key, records|
    records.each do |record|
      add key, record
    end
  end
end

.to_hash(item) ⇒ Object

Takes a DynamoDB item and returns the attributes of that item as a hash


233
234
235
# File 'lib/mince_dynamo_db/interface.rb', line 233

def self.to_hash(item)
  item.attributes if item
end

.update_field_with_value(collection_name, primary_key_value, field_name, new_value) ⇒ Object

Updates a field for a record that has the given value as its primary key

Parameters:

  • collection_name (String)

    the name of the collection

  • primary_key_value (String)

    the value of the primary key

  • field_name (String)

    the name of the field to update

  • new_value (String)

    the value to update the field with


74
75
76
77
# File 'lib/mince_dynamo_db/interface.rb', line 74

def self.update_field_with_value(collection_name, primary_key_value, field_name, new_value)
  item = find_item(collection_name, primary_key_value)
  item.set(field_name => new_value)
end