dynamo-record 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/dynamo-record/model.rb +97 -95
- data/lib/dynamo-record/record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d18ec7a057cdce73f1f6e3f6aafa7b0175580be9
|
4
|
+
data.tar.gz: b12e97171dc6df596a9c787750c463741295c626
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 509bdcf316a75601110e40ffaf80238d152c077d648f7433f4fef6566f458c9baad55e3cb66f75246166d54f2c9ab6e6f08b31128f92c0be6dac20dce47ff3ec
|
7
|
+
data.tar.gz: baf21793319609920a4526a027ebf2446a2af125e014717cf0493bcaa88ee97a0aad0af687fb1e4d5f892a0c342ed67afed9486006d0bab77c5bde3934010362
|
data/README.md
CHANGED
data/lib/dynamo-record/model.rb
CHANGED
@@ -1,122 +1,124 @@
|
|
1
1
|
require 'aws-record'
|
2
2
|
|
3
|
-
module DynamoRecord
|
4
|
-
|
3
|
+
module DynamoRecord
|
4
|
+
module Model
|
5
|
+
COMPOSITE_DELIMITER = '|'.freeze
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def self.included(klass)
|
8
|
+
klass.include(Aws::Record)
|
9
|
+
klass.include(DynamoRecord::Marshalers)
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
def table_name
|
16
|
-
[Rails.configuration.dynamo['prefix'], name.tableize].join('-').tr('/', '.')
|
11
|
+
klass.extend ClassMethods
|
12
|
+
klass.send :prepend, InstanceMethods
|
17
13
|
end
|
18
14
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
module ClassMethods
|
16
|
+
def table_name
|
17
|
+
[Rails.configuration.dynamo['prefix'], name.tableize].join('-').tr('/', '.')
|
18
|
+
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
name = self.name.demodulize
|
28
|
-
conditions = opts.map { |k, v| "#{k}=#{v}" }.join(', ')
|
29
|
-
error = "Couldn't find #{name} with #{conditions}"
|
30
|
-
raise Aws::Record::Errors::NotFound, error
|
31
|
-
end
|
20
|
+
def scan
|
21
|
+
raise 'no scanning in production' if Rails.env.production?
|
22
|
+
super
|
32
23
|
end
|
33
|
-
end
|
34
24
|
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
def find(opts)
|
26
|
+
super(opts).tap do |record|
|
27
|
+
unless record
|
28
|
+
name = self.name.demodulize
|
29
|
+
conditions = opts.map { |k, v| "#{k}=#{v}" }.join(', ')
|
30
|
+
error = "Couldn't find #{name} with #{conditions}"
|
31
|
+
raise Aws::Record::Errors::NotFound, error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
38
35
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
36
|
+
def find_all_by_hash_key(hash_key_value, opts = {})
|
37
|
+
find_all_by_index_and_hash_key(hash_key, hash_key_value, opts)
|
38
|
+
end
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
range_config: { name: gsi_config[:range_key], value: range_key_value },
|
49
|
-
index_name: gsi_name.to_s
|
50
|
-
)
|
51
|
-
end
|
40
|
+
def find_all_by_gsi_hash_key(gsi_name, hash_key_value, opts = {})
|
41
|
+
hash_key_name = global_secondary_indexes[gsi_name.to_sym][:hash_key]
|
42
|
+
find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts, gsi_name.to_s)
|
43
|
+
end
|
52
44
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
}
|
62
|
-
query_options[:index_name] = index_name if index_name
|
63
|
-
query_options.merge!(opts)
|
64
|
-
query(query_options)
|
65
|
-
end
|
45
|
+
def find_all_by_gsi_hash_and_range_keys(gsi_name, hash_key_value, range_key_value)
|
46
|
+
gsi_config = global_secondary_indexes[gsi_name.to_sym]
|
47
|
+
find_all_by_index_hash_and_range_keys(
|
48
|
+
hash_config: { name: gsi_config[:hash_key], value: hash_key_value },
|
49
|
+
range_config: { name: gsi_config[:range_key], value: range_key_value },
|
50
|
+
index_name: gsi_name.to_s
|
51
|
+
)
|
52
|
+
end
|
66
53
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
query(query_options)
|
81
|
-
end
|
54
|
+
def find_all_by_index_and_hash_key(hash_key_name, hash_key_value, opts = {}, index_name = nil)
|
55
|
+
query_options = {
|
56
|
+
select: 'ALL_ATTRIBUTES',
|
57
|
+
key_condition_expression: "#{hash_key_name} = :hash_key_value",
|
58
|
+
expression_attribute_values: {
|
59
|
+
':hash_key_value': hash_key_value
|
60
|
+
},
|
61
|
+
scan_index_forward: true
|
62
|
+
}
|
63
|
+
query_options[:index_name] = index_name if index_name
|
64
|
+
query_options.merge!(opts)
|
65
|
+
query(query_options)
|
66
|
+
end
|
82
67
|
|
83
|
-
|
84
|
-
|
85
|
-
|
68
|
+
def find_all_by_index_hash_and_range_keys(hash_config:, range_config:, index_name: nil,
|
69
|
+
scan_index_forward: true, limit: nil)
|
70
|
+
query_options = {
|
71
|
+
select: 'ALL_ATTRIBUTES',
|
72
|
+
key_condition_expression: "#{hash_config[:name]} = :hkv AND #{range_config[:name]} = :rkv",
|
73
|
+
expression_attribute_values: {
|
74
|
+
':hkv': hash_config[:value],
|
75
|
+
':rkv': range_config[:value]
|
76
|
+
},
|
77
|
+
scan_index_forward: scan_index_forward,
|
78
|
+
limit: limit
|
79
|
+
}
|
80
|
+
query_options[:index_name] = index_name if index_name
|
81
|
+
query(query_options)
|
82
|
+
end
|
86
83
|
|
87
|
-
|
88
|
-
|
89
|
-
|
84
|
+
def composite_key(*args)
|
85
|
+
args.join(COMPOSITE_DELIMITER)
|
86
|
+
end
|
90
87
|
|
91
|
-
|
92
|
-
|
93
|
-
|
88
|
+
def split_composite(string)
|
89
|
+
string.split(COMPOSITE_DELIMITER)
|
90
|
+
end
|
94
91
|
|
95
|
-
|
96
|
-
|
97
|
-
|
92
|
+
def find_all_by_model(model, opts = {})
|
93
|
+
find_all_by_hash_key(model.dynamo_key, opts)
|
94
|
+
end
|
98
95
|
|
99
|
-
|
100
|
-
|
101
|
-
|
96
|
+
def find_all_by_model_paginated(model, opts = {})
|
97
|
+
find_all_by_model(model, pagination_opts(opts))
|
98
|
+
end
|
102
99
|
|
103
|
-
|
104
|
-
|
100
|
+
def pagination_opts(opts = {})
|
101
|
+
{ limit: opts[:limit], exclusive_start_key: opts[:exclusive_start_key] }
|
102
|
+
end
|
105
103
|
|
106
|
-
|
107
|
-
def read_attribute_for_serialization(attribute)
|
108
|
-
send(attribute)
|
104
|
+
# TODO: Create a batch save method.
|
109
105
|
end
|
110
106
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
107
|
+
module InstanceMethods
|
108
|
+
def read_attribute_for_serialization(attribute)
|
109
|
+
send(attribute)
|
110
|
+
end
|
111
|
+
|
112
|
+
def attribute_hash
|
113
|
+
attrs = self.class.attributes.attributes
|
114
|
+
attr_keys = attrs.keys
|
115
|
+
hash = {}
|
115
116
|
|
116
|
-
|
117
|
-
|
117
|
+
attr_keys.each do |key|
|
118
|
+
hash[key] = attrs[key].type_cast(send(key))
|
119
|
+
end
|
120
|
+
hash
|
118
121
|
end
|
119
|
-
hash
|
120
122
|
end
|
121
123
|
end
|
122
124
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamo-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Davis McClellan
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: exe
|
15
15
|
cert_chain: []
|
16
|
-
date: 2017-05-
|
16
|
+
date: 2017-05-04 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: aws-record
|