dynamodb_model 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f4086919c79934456c55927f2c127cb983f3f16
4
- data.tar.gz: 00da845314f039d065ff7c122c7a7f7d85d33e08
3
+ metadata.gz: afe935d182837391daa3ca740b280d338f6bc458
4
+ data.tar.gz: 536563d9d5e036a46dfdcc3a1a9f47f6df24eff9
5
5
  SHA512:
6
- metadata.gz: c47587fa7275602096f3626175abfaf96e8f1c2e538c20558bcee5dc15c69218d92c3dc04c916afad8871a369e0fb1c2a8b84c866e80def1ec316fbffe870be4
7
- data.tar.gz: 4b9f9f3754e5d8ec6fd8b6492c40d877fa9d3d4c4b2a310f4addbf677046725c30534a81fc9c78fc57b4c4282db310f77280f483a067eb946c8ba205ecd40d88
6
+ metadata.gz: 6a7281afdba6068ec75af043d66c3490be35ed8beb39af7c0d60e13196bed14003a9c9142db616ca575e848fe7391467dcb70ba68a656ab5f74b5c28c205a118
7
+ data.tar.gz: c56348d9fda4a5ac732251e48f8924b50970488db1cfdb0a6090eb301a6084ec94bdce07f4f8e60168a4c538f7e335801cd05928f3fe693afe8afe5da0f78617
data/README.md CHANGED
@@ -55,8 +55,10 @@ emphasis that possibility.
55
55
  ### Delete
56
56
 
57
57
  ```ruby
58
- resp = Post.delete("myid")
59
- resp # dynamodb client resp
58
+ resp = Post.delete("myid") # dynamodb client resp
59
+ # or
60
+ post = Post.find("myid")
61
+ resp = post.delete # dynamodb client resp
60
62
  ```
61
63
 
62
64
  ### Scan
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_dependency "activesupport"
24
+ spec.add_dependency "aws-sdk-dynamodb"
24
25
 
25
26
  spec.add_development_dependency "bundler"
26
27
  spec.add_development_dependency "rake"
@@ -6,4 +6,7 @@ module DynamodbModel
6
6
  autoload :Dsl, "dynamodb_model/dsl"
7
7
  autoload :DbConfig, "dynamodb_model/db_config"
8
8
  autoload :Item, "dynamodb_model/item"
9
+ autoload :Util, "dynamodb_model/util"
10
+
11
+ extend Util
9
12
  end
@@ -1,3 +1,8 @@
1
+ require "aws-sdk-dynamodb"
2
+ require 'fileutils'
3
+ require 'erb'
4
+ require 'yaml'
5
+
1
6
  module DynamodbModel::DbConfig
2
7
  def self.included(base)
3
8
  base.extend(ClassMethods)
@@ -74,6 +74,10 @@ module DynamodbModel
74
74
  self.class.find(id)
75
75
  end
76
76
 
77
+ def delete
78
+ self.class.delete(@attrs[:id]) if @attrs[:id]
79
+ end
80
+
77
81
  def table_name
78
82
  self.class.table_name
79
83
  end
@@ -1,17 +1,16 @@
1
1
  module DynamodbModel
2
2
  class Migration
3
3
  autoload :Dsl, "dynamodb_model/migration/dsl"
4
+ autoload :Generator, "dynamodb_model/migration/generator"
4
5
 
5
- class << self
6
- def up
7
- puts "Running up migration for #{self.class.name}"
8
- end
6
+ def up
7
+ puts "Should defined an up method for your migration: #{self.class.name}"
8
+ end
9
9
 
10
- def create_table(table_name)
11
- dsl = Dsl.new(table_name)
12
- yield(dsl)
13
- dsl.execute
14
- end
10
+ def create_table(table_name)
11
+ dsl = Dsl.new(table_name)
12
+ yield(dsl)
13
+ dsl.execute
15
14
  end
16
15
  end
17
16
  end
@@ -91,9 +91,10 @@ class DynamodbModel::Migration
91
91
  end
92
92
  end
93
93
 
94
+ # http://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/dynamo-example-create-table.html
94
95
  def execute
95
96
  params = {
96
- table_name: @table_ame,
97
+ table_name: @table_name,
97
98
  key_schema: @key_schema,
98
99
  attribute_definitions: @attribute_definitions,
99
100
  provisioned_throughput: @provisioned_throughput
@@ -101,11 +102,9 @@ class DynamodbModel::Migration
101
102
  begin
102
103
  result = db.create_table(params)
103
104
 
104
- puts 'DynamoDB Table: proj-posts Status: ' +
105
- result.table_description.table_status;
105
+ puts "DynamoDB Table: #{@table_name} Status: #{result.table_description.table_status}"
106
106
  rescue Aws::DynamoDB::Errors::ServiceError => error
107
- puts 'Unable to create table:'
108
- puts error.message
107
+ puts "Unable to create table: #{error.message}"
109
108
  end
110
109
  end
111
110
  end
@@ -0,0 +1,45 @@
1
+ require "active_support/core_ext/string"
2
+
3
+ class DynamodbModel::Migration
4
+ # jets generate migration posts --partition-key id:string
5
+ class Generator
6
+ include DynamodbModel::DbConfig
7
+
8
+ attr_reader :table_name
9
+ def initialize(table_name, options)
10
+ @table_name = table_name.pluralize
11
+ @options = options
12
+ end
13
+
14
+ def generate
15
+ puts "Generating migration" unless @options[:quiet]
16
+ return if @options[:noop]
17
+ create_migration
18
+ end
19
+
20
+ def create_migration
21
+ migration_path = "#{DynamodbModel.root}db/migrate/#{migration_file_name}.rb"
22
+ dir = File.dirname(migration_path)
23
+ FileUtils.mkdir_p(dir) unless File.exist?(dir)
24
+ IO.write(migration_path, migration_code)
25
+ end
26
+
27
+ def migration_code
28
+ # @table_name already set
29
+ @migration_class_name = migration_file_name.classify
30
+ @partition_key = @options[:partition_key]
31
+ @sort_key = @options[:sort_key]
32
+ @provisioned_throughput = @options[:provisioned_throughput] || 5
33
+ template = IO.read(File.expand_path("../template.rb", __FILE__))
34
+ result = ERB.new(template, nil, "-").result(binding)
35
+ end
36
+
37
+ def migration_file_name
38
+ "#{@table_name}_migration"
39
+ end
40
+
41
+ def timestamp
42
+ "timestamp"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,59 @@
1
+ # Note: table name created will be namespaced based on
2
+ # DynamodbModel::Migration.table_namespace. This can be set in
3
+ # config/dynamodb.yml
4
+ #
5
+ # development:
6
+ # table_namespace: "mynamespace"
7
+ #
8
+ # This results in:
9
+ # create_table "posts" => table name: "mynamespace-posts"
10
+ #
11
+ # When you're in a in Jets project you can set the namespace based on
12
+ # Jets.config.table_namespace, which is based on the project name and
13
+ # a short version of the environment. Example:
14
+ #
15
+ # `config/dynamodb.yml`:
16
+ # development:
17
+ # table_namespace: <%%= Jets.config.table_namespace %>
18
+ #
19
+ # If your project_name is proj and environment is production:
20
+ # create_table "posts" => table name: "proj-prod-posts"
21
+ #
22
+ # If your project_name is proj and environment is staging:
23
+ # create_table "posts" => table name: "proj-stag-posts"
24
+ #
25
+ # If your project_name is proj and environment is development:
26
+ # create_table "posts" => table name: "proj-dev-posts"
27
+ #
28
+ # If the table_namespace is set to a blank string or nil, then a namespace
29
+ # will not be prepended at all.
30
+ class <%= @migration_class_name %> < DynamodbModel::Migration
31
+ def up
32
+ create_table :<%= @table_name %> do |t|
33
+ t.partition_key "<%= @partition_key %>" # required
34
+ <% if @sort_key # so extra spaces are not added when generated -%>
35
+ t.sort_key "<%= @sort_key %>" # optional
36
+ <% end -%>
37
+ t.provisioned_throughput(<%= @provisioned_throughput %>) # sets both read and write, defaults to 5 when not set
38
+
39
+ # Instead of using partition_key and sort_key you can set the
40
+ # key schema directly also
41
+ # t.key_schema([
42
+ # {attribute_name: "id", :key_type=>"HASH"},
43
+ # {attribute_name: "created_at", :key_type=>"RANGE"}
44
+ # ])
45
+ # t.attribute_definitions([
46
+ # {attribute_name: "id", attribute_type: "N"},
47
+ # {attribute_name: "created_at", attribute_type: "S"}
48
+ # ])
49
+
50
+ # other ways to set provisioned_throughput
51
+ # t.provisioned_throughput(:read, 10)
52
+ # t.provisioned_throughput(:write, 10)
53
+ # t.provisioned_throughput(
54
+ # read_capacity_units: 5,
55
+ # write_capacity_units: 5
56
+ # )
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ require 'logger'
2
+
3
+ module DynamodbModel::Util
4
+ # Ensures trailing slash
5
+ # Useful for appending a './' in front of a path or leaving it alone.
6
+ # Returns: '/path/with/trailing/slash/' or './'
7
+ @@root = nil
8
+ def root
9
+ return @@root if @@root
10
+ @@root = ENV['PROJECT_ROOT'].to_s
11
+ @@root = '.' if @@root == ''
12
+ @@root = "#{@@root}/" unless @@root.ends_with?('/')
13
+ @@root
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module DynamodbModel
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamodb_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-dynamodb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,6 +100,9 @@ files:
86
100
  - lib/dynamodb_model/item.rb
87
101
  - lib/dynamodb_model/migration.rb
88
102
  - lib/dynamodb_model/migration/dsl.rb
103
+ - lib/dynamodb_model/migration/generator.rb
104
+ - lib/dynamodb_model/migration/template.rb
105
+ - lib/dynamodb_model/util.rb
89
106
  - lib/dynamodb_model/version.rb
90
107
  homepage: https://github.com/tongueroo/dynamodb_model
91
108
  licenses: []