dynamodb_model 1.0.0 → 1.0.1

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: 355ffd3eb3d0d42649b31239c6297464be46d260
4
- data.tar.gz: e6ec2b09237a89b6e0c617d0e0f888c5578a52a5
3
+ metadata.gz: 695afa2fc15607d068003af7eab703194659ea0f
4
+ data.tar.gz: 4a0375284bf1565b781cd5fac752730e4fe7ddb4
5
5
  SHA512:
6
- metadata.gz: 57b068c88fbf47de849a3980be63a98411f0103bc979c2457248b71e3d17b7d7bec0fc9a45a3f99d7a939ac5f811f0c5ee814907def64105bd678465af5e3721
7
- data.tar.gz: c0c1ba569bbc4a1e411b3cc6821abd96a003a0fdba71dc56b6846f30275d3fd309ac8b6f524658b990045c1ec4745fd77880dbed2dc5a57665eecb847a4e0a45
6
+ metadata.gz: d26a55e6f07ae80457927212586ecd0fe1e3a5e7529e29c70db8b948d63c63bef07b75977dcdea0f52dbe3998bbd6eb1396758b58c84f0b3d3d152368a40fdbe
7
+ data.tar.gz: e10afd1105fce09364cdd41bfaa8bba11b43e489eaf63b082bd8c199726e87da56cc6e2fb29c12f969f2e7f4bf45e9d5215430f423505cde4cc97abf199eafd7
data/CHANGELOG.md CHANGED
@@ -3,6 +3,9 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [1.0.1]
7
+ - Check dynamodb local is running when configured
8
+
6
9
  ## [1.0.0]
7
10
  - LSI support
8
11
  - automatically infer table_name
data/README.md CHANGED
@@ -69,6 +69,26 @@ posts = Post.scan(options)
69
69
  posts # Array of Post items. [Post.new, Post.new, ...]
70
70
  ```
71
71
 
72
+ ### Query
73
+
74
+ ```ruby
75
+ posts = Post.query(
76
+ index_name: 'category-index',
77
+ expression_attribute_names: { "#category_name" => "category" },
78
+ expression_attribute_values: { ":category_value" => "Entertainment" },
79
+ key_condition_expression: "#category_name = :category_value",
80
+ )
81
+ posts # Array of Post items. [Post.new, Post.new, ...]
82
+ ```
83
+
84
+ ### Where
85
+
86
+ The where could be prettied up. Appreciate any pull requests.
87
+
88
+ ```ruby
89
+ Post.where({category: "Drama"}, {index_name: "category-index"})
90
+ ```
91
+
72
92
  Examples are also in [item_spec.rb](spec/lib/dynamodb_model/item_spec.rb).
73
93
 
74
94
  ## Migration Support
@@ -115,7 +135,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
115
135
 
116
136
  Bug reports and pull requests are welcome on GitHub at https://github.com/tongueroo/dynamodb_model.
117
137
 
118
- TODO
138
+ ### TODO
119
139
 
120
- * implement Post.query
140
+ * improve Post.where. Something like `Post.index_name("user_id").where(category_name: "Entertainment")` would be nice.
121
141
  * implement `post.update` with `db.update_item` in a Ruby-ish way
@@ -24,11 +24,40 @@ module DynamodbModel::DbConfig
24
24
 
25
25
  config = db_config
26
26
  endpoint = ENV['DYNAMODB_ENDPOINT'] || config['endpoint']
27
- Aws.config.update(endpoint: endpoint) if endpoint
27
+ check_dynamodb_local!(endpoint)
28
28
 
29
+ Aws.config.update(endpoint: endpoint) if endpoint
29
30
  @@db ||= Aws::DynamoDB::Client.new
30
31
  end
31
32
 
33
+ # When endoint has been configured to point at dynamodb local: localhost:8000
34
+ # check if port 8000 is listening and timeout quickly. Or else it takes a
35
+ # for DynamoDB local to time out, about 10 seconds...
36
+ # This wastes less of the users time.
37
+ def check_dynamodb_local!(endpoint)
38
+ return unless endpoint && endpoint.include?("8000")
39
+
40
+ open = port_open?("127.0.0.1", 8000, 0.2)
41
+ unless open
42
+ 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"
43
+ end
44
+ end
45
+
46
+ # Thanks: https://gist.github.com/ashrithr/5305786
47
+ def port_open?(ip, port, seconds=1)
48
+ # => checks if a port is open or not
49
+ Timeout::timeout(seconds) do
50
+ begin
51
+ TCPSocket.new(ip, port).close
52
+ true
53
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
54
+ false
55
+ end
56
+ end
57
+ rescue Timeout::Error
58
+ false
59
+ end
60
+
32
61
  # useful for specs
33
62
  def db=(db)
34
63
  @@db = db
@@ -156,7 +156,7 @@ module DynamodbModel
156
156
  # translates to
157
157
  #
158
158
  # resp = db.query(
159
- # table_name: "proj-dev-post",
159
+ # table_name: "demo-dev-post",
160
160
  # index_name: 'category-index',
161
161
  # expression_attribute_names: { "#category_name" => "category" },
162
162
  # expression_attribute_values: { ":category_value" => category },
@@ -159,7 +159,7 @@ class DynamodbModel::Migration
159
159
  end
160
160
  end
161
161
 
162
- # >> resp = Post.db.describe_table(table_name: "proj-dev-posts")
162
+ # >> resp = Post.db.describe_table(table_name: "demo-dev-posts")
163
163
  # >> resp.table.attribute_definitions.map(&:to_h)
164
164
  # => [{:attribute_name=>"id", :attribute_type=>"S"}]
165
165
  def gsi_attribute_definitions
@@ -1,7 +1,7 @@
1
1
  require "active_support/core_ext/string"
2
2
 
3
3
  class DynamodbModel::Migration
4
- # jets generate migration posts --partition-key id:string
4
+ # jets dynamodb generate posts --partition-key id:string
5
5
  class Generator
6
6
  include DynamodbModel::DbConfig
7
7
 
@@ -1,3 +1,3 @@
1
1
  module DynamodbModel
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
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: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen