Class: CredStash::Repository::DynamoDB
- Inherits:
-
Object
- Object
- CredStash::Repository::DynamoDB
- Defined in:
- lib/cred_stash/repository/dynamo_db.rb
Instance Method Summary collapse
- #delete(item) ⇒ Object
- #get(name) ⇒ Object
-
#initialize(client: nil) ⇒ DynamoDB
constructor
A new instance of DynamoDB.
- #list ⇒ Object
- #put(item) ⇒ Object
- #select(name, pluck: nil, limit: nil) ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize(client: nil) ⇒ DynamoDB
Returns a new instance of DynamoDB.
3 4 5 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 3 def initialize(client: nil) @client = client || Aws::DynamoDB::Client.new end |
Instance Method Details
#delete(item) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 68 def delete(item) @client.delete_item( table_name: CredStash.config.table_name, key: { name: item.name, version: item.version } ) end |
#get(name) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 7 def get(name) select(name, limit: 1).first.tap do |item| unless item raise CredStash::ItemNotFound, "#{name} is not found" end end end |
#list ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 58 def list @client.scan( table_name: CredStash.config.table_name, projection_expression: '#name, version', expression_attribute_names: { "#name" => "name" }, ).items.map do |item| Item.new(name: item['name'], version: item['version']) end end |
#put(item) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 43 def put(item) @client.put_item( table_name: CredStash.config.table_name, item: { name: item.name, version: item.version, key: item.key, contents: item.contents, hmac: item.hmac }, condition_expression: "attribute_not_exists(#name)", expression_attribute_names: { "#name" => "name" }, ) end |
#select(name, pluck: nil, limit: nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 15 def select(name, pluck: nil, limit: nil) params = { table_name: CredStash.config.table_name, consistent_read: true, key_condition_expression: "#name = :name", expression_attribute_names: { "#name" => "name"}, expression_attribute_values: { ":name" => name } } if pluck params[:projection_expression] = pluck end if limit params[:limit] = limit params[:scan_index_forward] = false end @client.query(params).items.map do |item| Item.new( key: item["key"], contents: item["contents"], name: item["name"], version: item["version"] ) end end |
#setup ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cred_stash/repository/dynamo_db.rb', line 78 def setup @client.create_table( table_name: CredStash.config.table_name, key_schema: [ { attribute_name: 'name', key_type: 'HASH' }, { attribute_name: 'version', key_type: 'RANGE' }, ], attribute_definitions: [ { attribute_name: 'name', attribute_type: 'S' }, { attribute_name: 'version', attribute_type: 'S' }, ], provisioned_throughput: { read_capacity_units: 1, write_capacity_units: 1, }, ) end |