hecks-adapters-dynamodb 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 62871074e4e03ee3ff985e1155f1abf8df99ac0c
4
+ data.tar.gz: b90704346fce146c43a7a398b7e800a3f8a2195e
5
+ SHA512:
6
+ metadata.gz: bb0aae14ddc831d6f9fda08d2e32c2d3131108dfade52e9c11ee8eeda497fb553831047ec7615086557a38c8453d060e9a815a04fd28339b528db8cd2a35dbe1
7
+ data.tar.gz: 9979e9627258e15f5e5d6d003ea5bb9b8741ad5cd345e988fd82142d14baef844c42a41ffec521ac587318755ae9c477fca9f1d1f103be2bcbff54320a56571d
@@ -0,0 +1,35 @@
1
+ module Hecks
2
+ module Adapters
3
+ class DynamoDB
4
+ module Commands
5
+ class Create
6
+ attr_reader :id
7
+
8
+ def initialize(args, head, client)
9
+ @args = args
10
+ @head = head
11
+ @client = client
12
+ end
13
+
14
+ def call
15
+ put_item
16
+ self
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :client, :args, :head
22
+
23
+ def put_item
24
+ @id = SecureRandom.uuid
25
+ client.put_item({
26
+ item: args.merge(id: @id),
27
+ return_consumed_capacity: 'TOTAL',
28
+ table_name: head.name
29
+ })
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ module Hecks
2
+ module Adapters
3
+ class DynamoDB
4
+ module Commands
5
+ class Delete
6
+ def initialize(id, head, client)
7
+ @head = head
8
+ @id = id
9
+ @client = client
10
+ end
11
+
12
+ def call
13
+ delete_item
14
+ self
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :client, :head
20
+
21
+ def delete_item
22
+ client.delete_item(
23
+ key: { "id" => @id },
24
+ table_name: head.name
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ module Hecks
2
+ module Adapters
3
+ class DynamoDB
4
+ module Commands
5
+ class Read
6
+ def initialize(id, head, client)
7
+ @client = client
8
+ @id = id
9
+ @head = head
10
+ end
11
+
12
+ def call
13
+ result = get_item
14
+ return nil if result.nil?
15
+ PizzaBuilder::Domain::Pizzas.head.new(result)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :client, :head
21
+
22
+ def get_item
23
+ symbolize(
24
+ client.get_item(
25
+ key: { id: @id },
26
+ table_name: head.name
27
+ ).item)
28
+ end
29
+
30
+ def symbolize(obj)
31
+ return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo} if obj.is_a? Hash
32
+ return obj.inject([]){|memo,v | memo << symbolize(v); memo} if obj.is_a? Array
33
+ return obj
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ module Hecks
2
+ module Adapters
3
+ class DynamoDB
4
+ module Commands
5
+ class Update
6
+ def initialize(id, attributes, head, client)
7
+ @head = head
8
+ @attributes = attributes
9
+ @client = client
10
+ @id = id
11
+ end
12
+
13
+ def call
14
+ update_item
15
+ self
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :head, :attributes, :client
21
+
22
+ def update_item
23
+ update_expression = "SET " + @attributes.map{|a| "##{a[0].upcase} = :#{a[0]}"}.join(", ")
24
+ attribute_names = @attributes.map{|a| ["##{a[0].upcase}", a[0].to_s]}.to_h
25
+ attribute_values = @attributes.map{|a| [":#{a[0]}", a[1]]}.to_h
26
+
27
+ client.update_item(
28
+ expression_attribute_names: attribute_names,
29
+ expression_attribute_values: attribute_values,
30
+ table_name: @head.name,
31
+ key: { id: @id },
32
+ return_values: "ALL_NEW",
33
+ update_expression: update_expression
34
+ )
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/drop_all.rb ADDED
@@ -0,0 +1,37 @@
1
+ module Hecks
2
+ module Adapters
3
+ class DynamoDB
4
+ class DropAll
5
+ def initialize(client:, domain:)
6
+ @client = client
7
+ @domain = domain
8
+ load(domain.spec_path)
9
+ end
10
+
11
+ def call
12
+ list_tables
13
+ drop_tables
14
+ self
15
+ end
16
+
17
+ def result
18
+ { tables: @tables }
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :tables, :client
24
+
25
+ def list_tables
26
+ @tables = client.list_tables.table_names
27
+ end
28
+
29
+ def drop_tables
30
+ @tables.each do |table|
31
+ client.delete_table({table_name: table})
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'repository'
2
+ require_relative 'migrate'
3
+ require_relative 'drop_all'
4
+
5
+ require 'aws-sdk'
6
+ require 'yaml'
7
+
8
+ module Hecks
9
+ module Adapters
10
+ class DynamoDB
11
+ def initialize(domain:)
12
+ @domain = domain
13
+ end
14
+
15
+ def [](value)
16
+ Repository.new(DOMAIN[value.to_s.titleize.to_sym].head)
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/migrate.rb ADDED
@@ -0,0 +1,54 @@
1
+ module Hecks
2
+ module Adapters
3
+ class DynamoDB
4
+ class Migrate
5
+ def initialize(client:, domain:)
6
+ @client = client
7
+ @unchanged_tables = []
8
+ @new_tables = []
9
+ @domain = domain
10
+ load(@domain.spec_path)
11
+ end
12
+
13
+ def call
14
+ create_tables
15
+ self
16
+ end
17
+
18
+ def result
19
+ { unchanged_tables: @unchanged_tables, new_tables: @new_tables }
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :client
25
+
26
+ def create_tables
27
+ DOMAIN.domain_modules.values.each do |domain_module|
28
+ create_table(domain_module.head.name)
29
+ end
30
+ end
31
+
32
+ def create_table(name)
33
+ begin
34
+ client.create_table(
35
+ attribute_definitions: [{
36
+ attribute_name: 'id',
37
+ attribute_type: 'S'
38
+ }],
39
+ table_name: name,
40
+ key_schema: [{ attribute_name: 'id', key_type: 'HASH' }],
41
+ provisioned_throughput: {
42
+ read_capacity_units: 5,
43
+ write_capacity_units: 5
44
+ }
45
+ )
46
+ @new_tables << name
47
+ rescue Aws::DynamoDB::Errors::ResourceInUseException => e
48
+ @unchanged_tables << name
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
data/lib/repository.rb ADDED
@@ -0,0 +1,39 @@
1
+ require_relative 'commands/create'
2
+ require_relative 'commands/read'
3
+ require_relative 'commands/update'
4
+ require_relative 'commands/delete'
5
+
6
+ module Hecks
7
+ module Adapters
8
+ class DynamoDB
9
+ class Repository
10
+ attr_reader :id
11
+
12
+ def initialize(head)
13
+ @head = head
14
+ @client = Aws::DynamoDB::Client.new(region: 'us-east-1')
15
+ end
16
+
17
+ def create(args)
18
+ Commands::Create.new(args, head, client).call
19
+ end
20
+
21
+ def delete(id)
22
+ Commands::Delete.new(id, head, client).call
23
+ end
24
+
25
+ def read(id)
26
+ Commands::Read.new(id, head, client).call
27
+ end
28
+
29
+ def update(id, attributes)
30
+ Commands::Update.new(id, attributes, head, client).call
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :head, :client
36
+ end
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hecks-adapters-dynamodb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - Chris Young
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ description: Hecks DynamoDB Database
28
+ email: chris@example.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/commands/create.rb
34
+ - lib/commands/delete.rb
35
+ - lib/commands/read.rb
36
+ - lib/commands/update.rb
37
+ - lib/drop_all.rb
38
+ - lib/hecks-adapters-dynamodb.rb
39
+ - lib/migrate.rb
40
+ - lib/repository.rb
41
+ homepage: https://github.com/chrisyoung/hecks
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.6.10
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Drive DynamoDB with Domains created with Hecks::Domain
65
+ test_files: []