jets 1.4.10 → 1.4.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitmodules +0 -3
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +6 -2
- data/lib/jets/application.rb +4 -0
- data/lib/jets/commands/templates/skeleton/Gemfile.tt +1 -0
- data/lib/jets/db.rb +11 -5
- data/lib/jets/resource/lambda/function.rb +7 -1
- data/lib/jets/version.rb +1 -1
- data/lib/jets.rb +0 -7
- data/vendor/jets-gems/lib/jets/gems/check.rb +2 -2
- metadata +2 -41
- data/vendor/dynomite/CHANGELOG.md +0 -45
- data/vendor/dynomite/Gemfile +0 -6
- data/vendor/dynomite/Gemfile.lock +0 -61
- data/vendor/dynomite/README.md +0 -141
- data/vendor/dynomite/Rakefile +0 -2
- data/vendor/dynomite/bin/console +0 -14
- data/vendor/dynomite/bin/setup +0 -8
- data/vendor/dynomite/docs/migrations/long-example.rb +0 -123
- data/vendor/dynomite/docs/migrations/short-example.rb +0 -40
- data/vendor/dynomite/dynomite.gemspec +0 -29
- data/vendor/dynomite/lib/dynomite/core.rb +0 -25
- data/vendor/dynomite/lib/dynomite/db_config.rb +0 -107
- data/vendor/dynomite/lib/dynomite/erb.rb +0 -53
- data/vendor/dynomite/lib/dynomite/item.rb +0 -292
- data/vendor/dynomite/lib/dynomite/log.rb +0 -15
- data/vendor/dynomite/lib/dynomite/migration/common.rb +0 -86
- data/vendor/dynomite/lib/dynomite/migration/dsl/base_secondary_index.rb +0 -73
- data/vendor/dynomite/lib/dynomite/migration/dsl/global_secondary_index.rb +0 -4
- data/vendor/dynomite/lib/dynomite/migration/dsl/local_secondary_index.rb +0 -8
- data/vendor/dynomite/lib/dynomite/migration/dsl.rb +0 -194
- data/vendor/dynomite/lib/dynomite/migration/executor.rb +0 -38
- data/vendor/dynomite/lib/dynomite/migration/generator.rb +0 -68
- data/vendor/dynomite/lib/dynomite/migration/templates/create_table.rb +0 -32
- data/vendor/dynomite/lib/dynomite/migration/templates/update_table.rb +0 -26
- data/vendor/dynomite/lib/dynomite/migration.rb +0 -27
- data/vendor/dynomite/lib/dynomite/version.rb +0 -3
- data/vendor/dynomite/lib/dynomite.rb +0 -23
- data/vendor/dynomite/pkg/dynomite-1.0.8.gem +0 -0
- data/vendor/dynomite/pkg/dynomite-1.0.9.gem +0 -0
- data/vendor/dynomite/pkg/dynomite-1.1.0.gem +0 -0
- data/vendor/dynomite/spec/fixtures/app_root/config/dynamodb.yml +0 -7
- data/vendor/dynomite/spec/fixtures/dynamodb/migrate/20190108061826-comments_migration.rb +0 -30
- data/vendor/dynomite/spec/lib/dynomite/item_spec.rb +0 -102
- data/vendor/dynomite/spec/lib/dynomite/migration/dsl/global_secondary_index_spec.rb +0 -106
- data/vendor/dynomite/spec/lib/dynomite/migration/dsl/local_secondary_index_spec.rb +0 -71
- data/vendor/dynomite/spec/lib/dynomite/migration/dsl_spec.rb +0 -76
- data/vendor/dynomite/spec/lib/dynomite/migration/generator_spec.rb +0 -23
- data/vendor/dynomite/spec/lib/dynomite/migration_spec.rb +0 -60
- data/vendor/dynomite/spec/spec_helper.rb +0 -110
@@ -1,40 +0,0 @@
|
|
1
|
-
class CreateCommentsMigration < Dynomite::Migration
|
2
|
-
def up
|
3
|
-
create_table :comments do |t|
|
4
|
-
t.partition_key "post_id:string" # required
|
5
|
-
t.sort_key "created_at:string" # optional
|
6
|
-
t.provisioned_throughput(1) # sets both read and write, defaults to 5 when not set
|
7
|
-
|
8
|
-
t.lsi do |i|
|
9
|
-
i.partition_key "post_id:string"
|
10
|
-
i.sort_key "updated_at:string" # required for lsi
|
11
|
-
i.provisioned_throughput(2)
|
12
|
-
end
|
13
|
-
|
14
|
-
# t.gsi do |i|
|
15
|
-
# i.partition_key "post_id:string"
|
16
|
-
# i.sort_key "deleted_at:string" # optional for gsi
|
17
|
-
# i.provisioned_throughput(2)
|
18
|
-
# end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class UpdateCommentsMigration < Dynomite::Migration
|
24
|
-
def up
|
25
|
-
update_table :comments do |t|
|
26
|
-
t.gsi(:create) do |i|
|
27
|
-
i.partition_key "post_id:string"
|
28
|
-
i.sort_key "flagged_at:string" # optional for gsi
|
29
|
-
i.provisioned_throughput(3)
|
30
|
-
end
|
31
|
-
|
32
|
-
t.gsi(:update, "update-me-index") do |i|
|
33
|
-
i.provisioned_throughput(5)
|
34
|
-
end
|
35
|
-
|
36
|
-
t.gsi(:delete, "delete-me-index")
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "dynomite/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "dynomite"
|
8
|
-
spec.version = Dynomite::VERSION
|
9
|
-
spec.authors = ["Tung Nguyen"]
|
10
|
-
spec.email = ["tongueroo@gmail.com"]
|
11
|
-
|
12
|
-
spec.summary = %q{ActiveRecord-ish Dynamodb Model}
|
13
|
-
spec.description = %q{ActiveRecord-ish Dynamodb Model}
|
14
|
-
spec.homepage = "https://github.com/tongueroo/dynomite"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
-
f.match(%r{^(test|spec|features)/})
|
18
|
-
end
|
19
|
-
spec.bindir = "exe"
|
20
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
-
spec.require_paths = ["lib"]
|
22
|
-
|
23
|
-
spec.add_dependency "activesupport"
|
24
|
-
spec.add_dependency "aws-sdk-dynamodb"
|
25
|
-
|
26
|
-
spec.add_development_dependency "bundler"
|
27
|
-
spec.add_development_dependency "rake"
|
28
|
-
spec.add_development_dependency "rspec"
|
29
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'logger'
|
2
|
-
|
3
|
-
module Dynomite::Core
|
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
|
-
@@app_root = nil
|
8
|
-
def app_root
|
9
|
-
return @@app_root if @@app_root
|
10
|
-
@@app_root = ENV['APP_ROOT'] || ENV['JETS_ROOT'] || ENV['RAILS_ROOT']
|
11
|
-
@@app_root = '.' if @@app_root.nil? || @app_root == ''
|
12
|
-
@@app_root = "#{@@app_root}/" unless @@app_root.ends_with?('/')
|
13
|
-
@@app_root
|
14
|
-
end
|
15
|
-
|
16
|
-
@@logger = nil
|
17
|
-
def logger
|
18
|
-
return @@logger if @@logger
|
19
|
-
@@logger = Logger.new($stderr)
|
20
|
-
end
|
21
|
-
|
22
|
-
def logger=(value)
|
23
|
-
@@logger = value
|
24
|
-
end
|
25
|
-
end
|
@@ -1,107 +0,0 @@
|
|
1
|
-
require "aws-sdk-dynamodb"
|
2
|
-
require 'fileutils'
|
3
|
-
require 'erb'
|
4
|
-
require 'yaml'
|
5
|
-
|
6
|
-
module Dynomite::DbConfig
|
7
|
-
def self.included(base)
|
8
|
-
base.extend(ClassMethods)
|
9
|
-
end
|
10
|
-
|
11
|
-
def db
|
12
|
-
self.class.db
|
13
|
-
end
|
14
|
-
|
15
|
-
# NOTE: Class including Dynomite::DbConfig is required to have table_name method defined
|
16
|
-
def namespaced_table_name
|
17
|
-
[self.class.table_namespace, table_name].reject {|s| s.nil? || s.empty?}.join('-')
|
18
|
-
end
|
19
|
-
|
20
|
-
module ClassMethods
|
21
|
-
@@db = nil
|
22
|
-
def db
|
23
|
-
return @@db if @@db
|
24
|
-
|
25
|
-
config = db_config
|
26
|
-
endpoint = ENV['DYNAMODB_ENDPOINT'] || config['endpoint']
|
27
|
-
check_dynamodb_local!(endpoint)
|
28
|
-
|
29
|
-
# Normally, do not set the endpoint to use the current configured region.
|
30
|
-
# Probably want to stay in the same region anyway for db connections.
|
31
|
-
#
|
32
|
-
# List of regional endpoints: https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region
|
33
|
-
# Example:
|
34
|
-
# endpoint: https://dynamodb.us-east-1.amazonaws.com
|
35
|
-
options = endpoint ? { endpoint: endpoint } : {}
|
36
|
-
@@db ||= Aws::DynamoDB::Client.new(options)
|
37
|
-
end
|
38
|
-
|
39
|
-
# When endoint has been configured to point at dynamodb local: localhost:8000
|
40
|
-
# check if port 8000 is listening and timeout quickly. Or else it takes a
|
41
|
-
# for DynamoDB local to time out, about 10 seconds...
|
42
|
-
# This wastes less of the users time.
|
43
|
-
def check_dynamodb_local!(endpoint)
|
44
|
-
return unless endpoint && endpoint.include?("8000")
|
45
|
-
|
46
|
-
open = port_open?("127.0.0.1", 8000, 0.2)
|
47
|
-
unless open
|
48
|
-
raise "You have configured your app to use DynamoDB local, but it is not running. Please start DynamoDB local. Example: brew cask install dynamodb-local && dynamodb-local"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# Thanks: https://gist.github.com/ashrithr/5305786
|
53
|
-
def port_open?(ip, port, seconds=1)
|
54
|
-
# => checks if a port is open or not
|
55
|
-
Timeout::timeout(seconds) do
|
56
|
-
begin
|
57
|
-
TCPSocket.new(ip, port).close
|
58
|
-
true
|
59
|
-
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
|
60
|
-
false
|
61
|
-
end
|
62
|
-
end
|
63
|
-
rescue Timeout::Error
|
64
|
-
false
|
65
|
-
end
|
66
|
-
|
67
|
-
# useful for specs
|
68
|
-
def db=(db)
|
69
|
-
@@db = db
|
70
|
-
end
|
71
|
-
|
72
|
-
def db_config
|
73
|
-
return @db_config if @db_config
|
74
|
-
|
75
|
-
if defined?(Jets)
|
76
|
-
config_path = "#{Jets.root}config/dynamodb.yml"
|
77
|
-
env = Jets.env
|
78
|
-
else
|
79
|
-
config_path = ENV['DYNOMITE_CONFIG'] || "./config/dynamodb.yml"
|
80
|
-
env = ENV['DYNOMITE_ENV'] || "development"
|
81
|
-
end
|
82
|
-
|
83
|
-
config = YAML.load(Dynomite::Erb.result(config_path))
|
84
|
-
@db_config ||= config[env] || {}
|
85
|
-
end
|
86
|
-
|
87
|
-
def table_namespace(*args)
|
88
|
-
case args.size
|
89
|
-
when 0
|
90
|
-
get_table_namespace
|
91
|
-
when 1
|
92
|
-
set_table_namespace(args[0])
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def get_table_namespace
|
97
|
-
return @table_namespace if defined?(@table_namespace)
|
98
|
-
|
99
|
-
config = db_config
|
100
|
-
@table_namespace = config['table_namespace']
|
101
|
-
end
|
102
|
-
|
103
|
-
def set_table_namespace(value)
|
104
|
-
@table_namespace = value
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'erb'
|
2
|
-
|
3
|
-
# Renders Erb and provide better backtrace where there's an error
|
4
|
-
#
|
5
|
-
# Usage:
|
6
|
-
#
|
7
|
-
# result = Dynomite::Erb.result(path, key1: "val1", key2: "val2")
|
8
|
-
#
|
9
|
-
class Dynomite::Erb
|
10
|
-
include Dynomite::Log
|
11
|
-
|
12
|
-
class << self
|
13
|
-
def result(path, variables={})
|
14
|
-
set_template_variables(variables)
|
15
|
-
template = IO.read(path)
|
16
|
-
begin
|
17
|
-
ERB.new(template, nil, "-").result(binding)
|
18
|
-
rescue Exception => e
|
19
|
-
log(e)
|
20
|
-
log(e.backtrace) if ENV['DEBUG']
|
21
|
-
|
22
|
-
# how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051
|
23
|
-
# syntax errors have the (erb):xxx info in e.message
|
24
|
-
# undefined variables have (erb):xxx info in e.backtrac
|
25
|
-
error_info = e.message.split("\n").grep(/\(erb\)/)[0]
|
26
|
-
error_info ||= e.backtrace.grep(/\(erb\)/)[0]
|
27
|
-
raise unless error_info # unable to find the (erb):xxx: error line
|
28
|
-
line = error_info.split(':')[1].to_i
|
29
|
-
log "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '').colorize(:green)}"
|
30
|
-
|
31
|
-
template_lines = template.split("\n")
|
32
|
-
context = 5 # lines of context
|
33
|
-
top, bottom = [line-context-1, 0].max, line+context-1
|
34
|
-
spacing = template_lines.size.to_s.size
|
35
|
-
template_lines[top..bottom].each_with_index do |line_content, index|
|
36
|
-
line_number = top+index+1
|
37
|
-
if line_number == line
|
38
|
-
printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content)
|
39
|
-
else
|
40
|
-
printf("%#{spacing}d %s\n", line_number, line_content)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
exit 1 unless ENV['TEST']
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def set_template_variables(variables)
|
48
|
-
variables.each do |key, value|
|
49
|
-
instance_variable_set(:"@#{key}", value)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,292 +0,0 @@
|
|
1
|
-
require "active_support/core_ext/hash"
|
2
|
-
require "aws-sdk-dynamodb"
|
3
|
-
require "digest"
|
4
|
-
require "yaml"
|
5
|
-
|
6
|
-
# The modeling is ActiveRecord-ish but not exactly because DynamoDB is a
|
7
|
-
# different type of database.
|
8
|
-
#
|
9
|
-
# Examples:
|
10
|
-
#
|
11
|
-
# post = MyModel.new
|
12
|
-
# post = post.replace(title: "test title")
|
13
|
-
#
|
14
|
-
# post.attrs[:id] now contain a generaetd unique partition_key id.
|
15
|
-
# Usually the partition_key is 'id'. You can set your own unique id also:
|
16
|
-
#
|
17
|
-
# post = MyModel.new(id: "myid", title: "my title")
|
18
|
-
# post.replace
|
19
|
-
#
|
20
|
-
# Note that the replace method replaces the entire item, so you
|
21
|
-
# need to merge the attributes if you want to keep the other attributes.
|
22
|
-
#
|
23
|
-
# post = MyModel.find("myid")
|
24
|
-
# post.attrs = post.attrs.deep_merge("desc": "my desc") # keeps title field
|
25
|
-
# post.replace
|
26
|
-
#
|
27
|
-
# The convenience `attrs` method performs a deep_merge:
|
28
|
-
#
|
29
|
-
# post = MyModel.find("myid")
|
30
|
-
# post.attrs("desc": "my desc") # <= does a deep_merge
|
31
|
-
# post.replace
|
32
|
-
#
|
33
|
-
# Note, a race condition edge case can exist when several concurrent replace
|
34
|
-
# calls are happening. This is why the interface is called replace to
|
35
|
-
# emphasis that possibility.
|
36
|
-
# TODO: implement post.update with db.update_item in a Ruby-ish way.
|
37
|
-
#
|
38
|
-
module Dynomite
|
39
|
-
class Item
|
40
|
-
include Log
|
41
|
-
include DbConfig
|
42
|
-
|
43
|
-
def initialize(attrs={})
|
44
|
-
@attrs = attrs
|
45
|
-
end
|
46
|
-
|
47
|
-
# Defining our own reader so we can do a deep merge if user passes in attrs
|
48
|
-
def attrs(*args)
|
49
|
-
case args.size
|
50
|
-
when 0
|
51
|
-
ActiveSupport::HashWithIndifferentAccess.new(@attrs)
|
52
|
-
when 1
|
53
|
-
attributes = args[0] # Hash
|
54
|
-
if attributes.empty?
|
55
|
-
ActiveSupport::HashWithIndifferentAccess.new
|
56
|
-
else
|
57
|
-
@attrs = attrs.deep_merge!(attributes)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
# Not using method_missing to allow usage of dot notation and assign
|
63
|
-
# @attrs because it might hide actual missing methods errors.
|
64
|
-
# DynamoDB attrs can go many levels deep so it makes less make sense to
|
65
|
-
# use to dot notation.
|
66
|
-
|
67
|
-
# The method is named replace to clearly indicate that the item is
|
68
|
-
# fully replaced.
|
69
|
-
def replace(hash={})
|
70
|
-
attrs = self.class.replace(@attrs.deep_merge(hash))
|
71
|
-
@attrs = attrs # refresh attrs because it now has the id
|
72
|
-
end
|
73
|
-
|
74
|
-
def find(id)
|
75
|
-
self.class.find(id)
|
76
|
-
end
|
77
|
-
|
78
|
-
def delete
|
79
|
-
self.class.delete(@attrs[:id]) if @attrs[:id]
|
80
|
-
end
|
81
|
-
|
82
|
-
def table_name
|
83
|
-
self.class.table_name
|
84
|
-
end
|
85
|
-
|
86
|
-
def partition_key
|
87
|
-
self.class.partition_key
|
88
|
-
end
|
89
|
-
|
90
|
-
# For render json: item
|
91
|
-
def as_json(options={})
|
92
|
-
@attrs
|
93
|
-
end
|
94
|
-
|
95
|
-
# Longer hand methods for completeness.
|
96
|
-
# Internallly encourage the shorter attrs method.
|
97
|
-
def attributes=(attributes)
|
98
|
-
@attributes = attributes
|
99
|
-
end
|
100
|
-
|
101
|
-
def attributes
|
102
|
-
@attributes
|
103
|
-
end
|
104
|
-
|
105
|
-
# Adds very little wrapper logic to scan.
|
106
|
-
#
|
107
|
-
# * Automatically add table_name to options for convenience.
|
108
|
-
# * Decorates return value. Returns Array of [MyModel.new] instead of the
|
109
|
-
# dynamodb client response.
|
110
|
-
#
|
111
|
-
# Other than that, usage is same was using the dynamodb client scan method
|
112
|
-
# directly. Example:
|
113
|
-
#
|
114
|
-
# MyModel.scan(
|
115
|
-
# expression_attribute_names: {"#updated_at"=>"updated_at"},
|
116
|
-
# filter_expression: "#updated_at between :start_time and :end_time",
|
117
|
-
# expression_attribute_values: {
|
118
|
-
# ":start_time" => "2010-01-01T00:00:00",
|
119
|
-
# ":end_time" => "2020-01-01T00:00:00"
|
120
|
-
# }
|
121
|
-
# )
|
122
|
-
#
|
123
|
-
# AWS Docs examples: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Ruby.04.html
|
124
|
-
def self.scan(params={})
|
125
|
-
log("It's recommended to not use scan for production. It can be slow and expensive. You can a LSI or GSI and query the index instead.")
|
126
|
-
log("Scanning table: #{table_name}")
|
127
|
-
params = { table_name: table_name }.merge(params)
|
128
|
-
resp = db.scan(params)
|
129
|
-
resp.items.map {|i| self.new(i) }
|
130
|
-
end
|
131
|
-
|
132
|
-
# Adds very little wrapper logic to query.
|
133
|
-
#
|
134
|
-
# * Automatically add table_name to options for convenience.
|
135
|
-
# * Decorates return value. Returns Array of [MyModel.new] instead of the
|
136
|
-
# dynamodb client response.
|
137
|
-
#
|
138
|
-
# Other than that, usage is same was using the dynamodb client query method
|
139
|
-
# directly. Example:
|
140
|
-
#
|
141
|
-
# MyModel.query(
|
142
|
-
# index_name: 'category-index',
|
143
|
-
# expression_attribute_names: { "#category_name" => "category" },
|
144
|
-
# expression_attribute_values: { ":category_value" => "Entertainment" },
|
145
|
-
# key_condition_expression: "#category_name = :category_value",
|
146
|
-
# )
|
147
|
-
#
|
148
|
-
# AWS Docs examples: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Ruby.04.html
|
149
|
-
def self.query(params={})
|
150
|
-
params = { table_name: table_name }.merge(params)
|
151
|
-
resp = db.query(params)
|
152
|
-
resp.items.map {|i| self.new(i) }
|
153
|
-
end
|
154
|
-
|
155
|
-
# Translates simple query searches:
|
156
|
-
#
|
157
|
-
# Post.where({category: "Drama"}, index_name: "category-index")
|
158
|
-
#
|
159
|
-
# translates to
|
160
|
-
#
|
161
|
-
# resp = db.query(
|
162
|
-
# table_name: "demo-dev-post",
|
163
|
-
# index_name: 'category-index',
|
164
|
-
# expression_attribute_names: { "#category_name" => "category" },
|
165
|
-
# expression_attribute_values: { ":category_value" => category },
|
166
|
-
# key_condition_expression: "#category_name = :category_value",
|
167
|
-
# )
|
168
|
-
#
|
169
|
-
# TODO: Implement nicer where syntax with index_name as a chained method.
|
170
|
-
#
|
171
|
-
# Post.where({category: "Drama"}, {index_name: "category-index"})
|
172
|
-
# VS
|
173
|
-
# Post.where(category: "Drama").index_name("category-index")
|
174
|
-
def self.where(attributes, options={})
|
175
|
-
raise "attributes.size == 1 only supported for now" if attributes.size != 1
|
176
|
-
|
177
|
-
attr_name = attributes.keys.first
|
178
|
-
attr_value = attributes[attr_name]
|
179
|
-
|
180
|
-
# params = {
|
181
|
-
# expression_attribute_names: { "#category_name" => "category" },
|
182
|
-
# expression_attribute_values: { ":category_value" => "Entertainment" },
|
183
|
-
# key_condition_expression: "#category_name = :category_value",
|
184
|
-
# }
|
185
|
-
name_key, value_key = "##{attr_name}_name", ":#{attr_name}_value"
|
186
|
-
params = {
|
187
|
-
expression_attribute_names: { name_key => attr_name },
|
188
|
-
expression_attribute_values: { value_key => attr_value },
|
189
|
-
key_condition_expression: "#{name_key} = #{value_key}",
|
190
|
-
}
|
191
|
-
# Allow direct access to override params passed to dynamodb query options.
|
192
|
-
# This is is how index_name is passed:
|
193
|
-
params = params.merge(options)
|
194
|
-
|
195
|
-
query(params)
|
196
|
-
end
|
197
|
-
|
198
|
-
def self.replace(attrs)
|
199
|
-
# Automatically adds some attributes:
|
200
|
-
# partition key unique id
|
201
|
-
# created_at and updated_at timestamps. Timestamp format from AWS docs: http://amzn.to/2z98Bdc
|
202
|
-
defaults = {
|
203
|
-
partition_key => Digest::SHA1.hexdigest([Time.now, rand].join)
|
204
|
-
}
|
205
|
-
item = defaults.merge(attrs)
|
206
|
-
item["created_at"] ||= Time.now.utc.strftime('%Y-%m-%dT%TZ')
|
207
|
-
item["updated_at"] = Time.now.utc.strftime('%Y-%m-%dT%TZ')
|
208
|
-
|
209
|
-
# put_item full replaces the item
|
210
|
-
resp = db.put_item(
|
211
|
-
table_name: table_name,
|
212
|
-
item: item
|
213
|
-
)
|
214
|
-
|
215
|
-
# The resp does not contain the attrs. So might as well return
|
216
|
-
# the original item with the generated partition_key value
|
217
|
-
item
|
218
|
-
end
|
219
|
-
|
220
|
-
def self.find(id)
|
221
|
-
resp = db.get_item(
|
222
|
-
table_name: table_name,
|
223
|
-
key: {partition_key => id}
|
224
|
-
)
|
225
|
-
attributes = resp.item # unwraps the item's attributes
|
226
|
-
self.new(attributes) if attributes
|
227
|
-
end
|
228
|
-
|
229
|
-
# Two ways to use the delete method:
|
230
|
-
#
|
231
|
-
# 1. Specify the key as a String. In this case the key will is the partition_key
|
232
|
-
# set on the model.
|
233
|
-
# MyModel.delete("728e7b5df40b93c3ea6407da8ac3e520e00d7351")
|
234
|
-
#
|
235
|
-
# 2. Specify the key as a Hash, you can arbitrarily specific the key structure this way
|
236
|
-
# MyModel.delete("728e7b5df40b93c3ea6407da8ac3e520e00d7351")
|
237
|
-
#
|
238
|
-
# options is provided in case you want to specific condition_expression or
|
239
|
-
# expression_attribute_values.
|
240
|
-
def self.delete(key_object, options={})
|
241
|
-
if key_object.is_a?(String)
|
242
|
-
key = {
|
243
|
-
partition_key => key_object
|
244
|
-
}
|
245
|
-
else # it should be a Hash
|
246
|
-
key = key_object
|
247
|
-
end
|
248
|
-
|
249
|
-
params = {
|
250
|
-
table_name: table_name,
|
251
|
-
key: key
|
252
|
-
}
|
253
|
-
# In case you want to specify condition_expression or expression_attribute_values
|
254
|
-
params = params.merge(options)
|
255
|
-
|
256
|
-
resp = db.delete_item(params)
|
257
|
-
end
|
258
|
-
|
259
|
-
# When called with an argument we'll set the internal @partition_key value
|
260
|
-
# When called without an argument just retun it.
|
261
|
-
# class Comment < Dynomite::Item
|
262
|
-
# partition_key "post_id"
|
263
|
-
# end
|
264
|
-
def self.partition_key(*args)
|
265
|
-
case args.size
|
266
|
-
when 0
|
267
|
-
@partition_key || "id" # defaults to id
|
268
|
-
when 1
|
269
|
-
@partition_key = args[0].to_s
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
def self.table_name(*args)
|
274
|
-
case args.size
|
275
|
-
when 0
|
276
|
-
get_table_name
|
277
|
-
when 1
|
278
|
-
set_table_name(args[0])
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
def self.get_table_name
|
283
|
-
@table_name ||= self.name.pluralize.underscore
|
284
|
-
[table_namespace, @table_name].reject {|s| s.nil? || s.empty?}.join('-')
|
285
|
-
end
|
286
|
-
|
287
|
-
def self.set_table_name(value)
|
288
|
-
@table_name = value
|
289
|
-
end
|
290
|
-
|
291
|
-
end
|
292
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
# Common methods to the *SecondaryIndex classes that handle gsi and lsi methods
|
2
|
-
# as well a the Dsl class that handles create_table and update_table methods.
|
3
|
-
class Dynomite::Migration::Dsl
|
4
|
-
module Common
|
5
|
-
# http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Types/KeySchemaElement.html
|
6
|
-
# partition_key is required
|
7
|
-
def partition_key(identifier)
|
8
|
-
@partition_key_identifier = identifier # for later use. useful for conventional_index_name
|
9
|
-
adjust_schema_and_attributes(identifier, "hash")
|
10
|
-
end
|
11
|
-
|
12
|
-
# sort_key is optional
|
13
|
-
def sort_key(identifier)
|
14
|
-
@sort_key_identifier = identifier # for later use. useful for conventional_index_name
|
15
|
-
adjust_schema_and_attributes(identifier, "range")
|
16
|
-
end
|
17
|
-
|
18
|
-
# Parameters:
|
19
|
-
# identifier: "id:string" or "id"
|
20
|
-
# key_type: "hash" or "range"
|
21
|
-
#
|
22
|
-
# Adjusts the parameters for create_table to add the
|
23
|
-
# partition_key and sort_key
|
24
|
-
def adjust_schema_and_attributes(identifier, key_type)
|
25
|
-
name, attribute_type = identifier.split(':')
|
26
|
-
attribute_type = "string" if attribute_type.nil?
|
27
|
-
|
28
|
-
partition_key = {
|
29
|
-
attribute_name: name,
|
30
|
-
key_type: key_type.upcase
|
31
|
-
}
|
32
|
-
@key_schema << partition_key
|
33
|
-
|
34
|
-
attribute_definition = {
|
35
|
-
attribute_name: name,
|
36
|
-
attribute_type: Dynomite::ATTRIBUTE_TYPES[attribute_type]
|
37
|
-
}
|
38
|
-
@attribute_definitions << attribute_definition
|
39
|
-
end
|
40
|
-
|
41
|
-
# t.provisioned_throughput(5) # both
|
42
|
-
# t.provisioned_throughput(:read, 5)
|
43
|
-
# t.provisioned_throughput(:write, 5)
|
44
|
-
# t.provisioned_throughput(:both, 5)
|
45
|
-
def provisioned_throughput(*params)
|
46
|
-
case params.size
|
47
|
-
when 0 # reader method
|
48
|
-
return @provisioned_throughput # early return
|
49
|
-
when 1
|
50
|
-
# @provisioned_throughput_set_called useful for update_table
|
51
|
-
# only provide a provisioned_throughput settings if explicitly called for update_table
|
52
|
-
@provisioned_throughput_set_called = true
|
53
|
-
arg = params[0]
|
54
|
-
if arg.is_a?(Hash)
|
55
|
-
# Case:
|
56
|
-
# provisioned_throughput(
|
57
|
-
# read_capacity_units: 10,
|
58
|
-
# write_capacity_units: 10
|
59
|
-
# )
|
60
|
-
@provisioned_throughput = arg # set directly
|
61
|
-
return # early return
|
62
|
-
else # assume parameter is an Integer
|
63
|
-
# Case: provisioned_throughput(10)
|
64
|
-
capacity_type = :both
|
65
|
-
capacity_units = arg
|
66
|
-
end
|
67
|
-
when 2
|
68
|
-
@provisioned_throughput_set_called = true
|
69
|
-
# Case: provisioned_throughput(:read, 5)
|
70
|
-
capacity_type, capacity_units = params
|
71
|
-
end
|
72
|
-
|
73
|
-
map = {
|
74
|
-
read: :read_capacity_units,
|
75
|
-
write: :write_capacity_units,
|
76
|
-
}
|
77
|
-
|
78
|
-
if capacity_type == :both
|
79
|
-
@provisioned_throughput[map[:read]] = capacity_units
|
80
|
-
@provisioned_throughput[map[:write]] = capacity_units
|
81
|
-
else
|
82
|
-
@provisioned_throughput[capacity_type] = capacity_units
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|