dynameek 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -6
- data/lib/dynameek/model/query.rb +25 -17
- data/lib/dynameek/model/structure.rb +1 -17
- data/lib/dynameek/model.rb +0 -15
- data/lib/dynameek/version.rb +1 -1
- data/test/spec/conversion_spec.rb +2 -2
- metadata +1 -1
data/README.md
CHANGED
@@ -9,7 +9,8 @@ A very lightweight ORMish model thing for amazon's dynamo db, after initialising
|
|
9
9
|
|
10
10
|
You can create table models with this kind of syntax:
|
11
11
|
|
12
|
-
class Conversion
|
12
|
+
class Conversion
|
13
|
+
include Dynameek::Model
|
13
14
|
|
14
15
|
field :client_id, :integer
|
15
16
|
field :channel_id, :string
|
@@ -25,10 +26,16 @@ Creation of the models is as you'd expect
|
|
25
26
|
|
26
27
|
con = Conversion.create(:client_id => 1, :channel_id => "google", :goal_name=> "Some Goal", :time => DateTime.now)
|
27
28
|
|
28
|
-
The models can be edited like normal (
|
29
|
+
The models can be edited like normal (*no update_attributes yet though*)
|
29
30
|
|
30
31
|
con.goal_name="hello"
|
31
32
|
con.save
|
33
|
+
|
34
|
+
Deletion also happens as you'd expect
|
35
|
+
|
36
|
+
con.delete
|
37
|
+
Conversion.delete_table
|
38
|
+
Conversion.query(["1", "google"]).delete
|
32
39
|
|
33
40
|
These models can be queried by find and query, although this is still undergoing some refactoring at the moment it currently looks something like this:
|
34
41
|
|
@@ -36,10 +43,9 @@ These models can be queried by find and query, although this is still undergoing
|
|
36
43
|
|
37
44
|
Conversion.query(["1", "google"]).where(DateTime.now, :lt).where(DateTime.now - 10, :gte).all
|
38
45
|
|
39
|
-
|
40
|
-
|
46
|
+
> NB. The where clauses are only for referencing the range part of the composite hash key, there is currently no way to search by
|
47
|
+
> hash contents as that felt like it was against the point of a document store.
|
41
48
|
|
42
49
|
### Disclaimery Bit
|
43
50
|
|
44
|
-
|
45
|
-
to clone down the project and modify it for your own use.
|
51
|
+
The gem is dynameek (unsurprisingly) but I wouldn't use it yet, far better to clone down the project and modify it for your own use.
|
data/lib/dynameek/model/query.rb
CHANGED
@@ -2,13 +2,12 @@ module Dynameek
|
|
2
2
|
module Model
|
3
3
|
module Query
|
4
4
|
#Similar to dynamoids approach but lighter (hopefully)
|
5
|
-
|
6
5
|
class QueryChain
|
7
6
|
|
8
7
|
def initialize(model)
|
9
8
|
@model = model
|
10
9
|
@hash_key = nil
|
11
|
-
@range = {eq: nil,
|
10
|
+
@range = {eq: nil, gte: nil, gt: nil, lte: nil, lt: nil}
|
12
11
|
end
|
13
12
|
|
14
13
|
def query(hash_key)
|
@@ -45,12 +44,11 @@ module Dynameek
|
|
45
44
|
RANGE_QUERY_MAP =
|
46
45
|
{
|
47
46
|
eq: :range_value,
|
48
|
-
within: :range_value,
|
49
47
|
gte: :range_gte,
|
50
48
|
gt: :range_greater_than,
|
51
49
|
lte: :range_lte,
|
52
50
|
lt: :range_less_than,
|
53
|
-
begins_with: :range_begins_with
|
51
|
+
# begins_with: :range_begins_with
|
54
52
|
}
|
55
53
|
|
56
54
|
private
|
@@ -61,24 +59,34 @@ module Dynameek
|
|
61
59
|
|
62
60
|
query_hash = {:hash_value => hash_key, :select => :all}
|
63
61
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
62
|
+
range = @range.select{|key, val| !val.nil?}
|
63
|
+
if range.size == 2
|
64
|
+
#only makes sense if one is a gt and the other a lt
|
65
|
+
multiple_gt = (range.keys & [:gte, :gt]).size == 2
|
66
|
+
multiple_lt = (range.keys & [:lte, :lt]).size == 2
|
67
|
+
raise Exception.new("Query cannot have multiple greater than operations") if multiple_gt
|
68
|
+
raise Exception.new("Query cannot have multiple less than operations") if multiple_lt
|
69
|
+
start_of_range = range[:gte] || range[:gt].succ
|
70
|
+
end_of_range = range[:lte] || range[:lt]
|
71
|
+
|
72
|
+
converted_start_of_range = @model.convert_to_dynamodb(@model.range_info.type, start_of_range)
|
73
|
+
converted_end_of_range = @model.convert_to_dynamodb(@model.range_info.type, end_of_range)
|
74
|
+
|
75
|
+
range[:range_value] = Range.new(converted_start_of_range, converted_end_of_range, (range[:lte].nil?))
|
76
|
+
range.delete(:lt)
|
77
|
+
range.delete(:lte)
|
78
|
+
range.delete(:gt)
|
79
|
+
range.delete(:gte)
|
80
|
+
elsif range.size > 2
|
81
|
+
raise Exception.new("Dynameek does not currently support more than two range querys")
|
82
|
+
else
|
83
|
+
range = range.reduce({}) {|memo, (key, val)| memo[RANGE_QUERY_MAP[key]] = @model.convert_to_dynamodb(@model.range_info.type, val); memo}
|
69
84
|
end
|
70
|
-
|
71
|
-
# "CONTENTS"
|
72
|
-
# @model.table.items.each{|item| p item.inspect}
|
73
|
-
# p "_________"
|
85
|
+
query_hash.merge!(range)
|
74
86
|
@model.table.items.query(query_hash).map{|item|
|
75
|
-
# p item.inspect
|
76
87
|
@model.item_to_instance(item)
|
77
88
|
}
|
78
|
-
|
79
89
|
end
|
80
|
-
|
81
|
-
|
82
90
|
end
|
83
91
|
|
84
92
|
[:query, :where, :all, :each, :each_with_index, :delete].each do |method|
|
@@ -2,7 +2,6 @@ module Dynameek
|
|
2
2
|
module Model
|
3
3
|
module Structure
|
4
4
|
|
5
|
-
|
6
5
|
def fields
|
7
6
|
@fields ||= {}
|
8
7
|
end
|
@@ -46,9 +45,7 @@ module Dynameek
|
|
46
45
|
def multi_column_hash_key_fields
|
47
46
|
@multi_column_hash_key_fields ||= []
|
48
47
|
end
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
|
52
49
|
def multi_column_join
|
53
50
|
@multi_column_join ||= "|"
|
54
51
|
end
|
@@ -70,8 +67,6 @@ module Dynameek
|
|
70
67
|
define_method("#{fieldname.to_s}=") {|value| write_attribute(fieldname, value) }
|
71
68
|
end
|
72
69
|
|
73
|
-
|
74
|
-
|
75
70
|
def hash_key fieldname
|
76
71
|
fieldname = fieldname.to_sym
|
77
72
|
check_field(fieldname)
|
@@ -79,10 +74,6 @@ module Dynameek
|
|
79
74
|
hash_key_info.type = fields[fieldname]
|
80
75
|
define_method(:hash_key) { read_attribute(fieldname) }
|
81
76
|
end
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
77
|
|
87
78
|
def multi_column_hash_key fieldnames
|
88
79
|
fieldnames = fieldnames.map(&:to_sym)
|
@@ -100,8 +91,6 @@ module Dynameek
|
|
100
91
|
hash_key_info.field = fieldname
|
101
92
|
hash_key_info.type = :string
|
102
93
|
end
|
103
|
-
|
104
|
-
|
105
94
|
|
106
95
|
def range fieldname
|
107
96
|
fieldname = fieldname.to_sym
|
@@ -110,11 +99,6 @@ module Dynameek
|
|
110
99
|
range_info.type = fields[fieldname]
|
111
100
|
end
|
112
101
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
102
|
def check_field(fieldname)
|
119
103
|
raise ("#{fieldname} is not a recognised field") if fields[fieldname].nil?
|
120
104
|
end
|
data/lib/dynameek/model.rb
CHANGED
@@ -22,8 +22,6 @@ module Dynameek
|
|
22
22
|
self
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
25
|
def attributes
|
28
26
|
@attributes ||= {}
|
29
27
|
end
|
@@ -62,31 +60,24 @@ module Dynameek
|
|
62
60
|
include Dynameek::Model::DynamoDb
|
63
61
|
include Dynameek::Model::Structure
|
64
62
|
include Dynameek::Model::Query
|
65
|
-
|
66
|
-
|
67
|
-
|
68
63
|
|
69
64
|
def find(hash_key, range_val=nil)
|
70
65
|
raise Exception("This has a composite hash with a range, the range val is required") if(range_val.nil? && range?)
|
71
66
|
#multicolumn
|
72
67
|
hash_key = hash_key.join(multi_column_join) if(hash_key.is_a?(Array))
|
73
|
-
|
74
68
|
items = if range?
|
75
69
|
range_val = convert_to_dynamodb(range_info.type, range_val)
|
76
70
|
table.batch_get(:all, [[hash_key, range_val]])
|
77
71
|
else
|
78
72
|
table.batch_get(:all, [hash_key])
|
79
73
|
end
|
80
|
-
# p items.methods - Object.new.methods
|
81
74
|
return nil if(items.entries.size == 0)
|
82
75
|
item_to_instance(items.first)
|
83
|
-
|
84
76
|
end
|
85
77
|
|
86
78
|
def delete_table
|
87
79
|
table.delete
|
88
80
|
end
|
89
|
-
|
90
81
|
|
91
82
|
def item_to_instance(item)
|
92
83
|
item_hsh = (item.is_a?(AWS::DynamoDB::Item) || item.is_a?(AWS::DynamoDB::ItemData) ? item.attributes.to_hash : item)
|
@@ -101,8 +92,6 @@ module Dynameek
|
|
101
92
|
instance
|
102
93
|
end
|
103
94
|
|
104
|
-
|
105
|
-
|
106
95
|
def create(attrib)
|
107
96
|
instance = self.new
|
108
97
|
attrib.each do |key, val|
|
@@ -119,14 +108,10 @@ module Dynameek
|
|
119
108
|
before_save_callbacks << method.to_sym
|
120
109
|
end
|
121
110
|
|
122
|
-
|
123
|
-
|
124
111
|
def table_name
|
125
112
|
self.to_s
|
126
113
|
end
|
127
114
|
|
128
|
-
|
129
|
-
|
130
115
|
end
|
131
116
|
end
|
132
117
|
end
|
data/lib/dynameek/version.rb
CHANGED
@@ -50,8 +50,8 @@ describe Conversion do
|
|
50
50
|
cons.size.should == 3
|
51
51
|
cons = Conversion.query([1, "google"]).where(now - 4, :gt).all
|
52
52
|
cons.size.should == 3
|
53
|
-
|
54
|
-
|
53
|
+
cons = Conversion.query([1, "google"]).where(now - 2, :gte).where(now, :lt).all
|
54
|
+
cons.size.should == 2
|
55
55
|
end
|
56
56
|
|
57
57
|
|