dynamo_record 0.0.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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +7 -0
  7. data/dynamo_record.gemspec +28 -0
  8. data/lib/dynamo_record/collection.rb +31 -0
  9. data/lib/dynamo_record/config.rb +15 -0
  10. data/lib/dynamo_record/document.rb +32 -0
  11. data/lib/dynamo_record/fields.rb +31 -0
  12. data/lib/dynamo_record/finders.rb +17 -0
  13. data/lib/dynamo_record/persistence.rb +85 -0
  14. data/lib/dynamo_record/query.rb +29 -0
  15. data/lib/dynamo_record/version.rb +3 -0
  16. data/lib/dynamo_record.rb +25 -0
  17. data/spec/app/models/address.rb +5 -0
  18. data/spec/app/models/person.rb +5 -0
  19. data/spec/dynamo_record/collection_spec.rb +21 -0
  20. data/spec/dynamo_record/config_spec.rb +25 -0
  21. data/spec/dynamo_record/document_spec.rb +19 -0
  22. data/spec/dynamo_record/fields_spec.rb +29 -0
  23. data/spec/dynamo_record/finders_spec.rb +20 -0
  24. data/spec/dynamo_record/persistence_spec.rb +89 -0
  25. data/spec/dynamo_record/query_spec.rb +58 -0
  26. data/spec/dynamo_record_spec.rb +5 -0
  27. data/spec/fixtures/vcr_cassettes/DynamoRecord_Document/initializes_from_database.yml +52 -0
  28. data/spec/fixtures/vcr_cassettes/DynamoRecord_Fields/_find/finds_record.yml +52 -0
  29. data/spec/fixtures/vcr_cassettes/DynamoRecord_Fields/_find/when_record_doesn_t_exists/returns_empty_object.yml +52 -0
  30. data/spec/fixtures/vcr_cassettes/DynamoRecord_Persistence/_create_table/with_index/creates_table.yml +52 -0
  31. data/spec/fixtures/vcr_cassettes/DynamoRecord_Persistence/_create_table/without_index/creates_table.yml +52 -0
  32. data/spec/fixtures/vcr_cassettes/DynamoRecord_Persistence/does_not_overwrite_existing_record.yml +103 -0
  33. data/spec/fixtures/vcr_cassettes/DynamoRecord_Persistence/updates_record.yml +150 -0
  34. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/find_all_records.yml +54 -0
  35. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/find_all_records_with_limie.yml +52 -0
  36. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/find_all_records_with_limit.yml +52 -0
  37. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/paginates_records.yml +52 -0
  38. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/pagination/enumerates_all_pages.yml +199 -0
  39. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/pagination/navigates_to_next_page.yml +101 -0
  40. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_all/pagination/tells_if_there_is_a_next_page.yml +52 -0
  41. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/_query/queries_by_condition.yml +53 -0
  42. data/spec/fixtures/vcr_cassettes/DynamoRecord_Query/querying/_where/queries_by_condition.yml +53 -0
  43. data/spec/spec_helper.rb +18 -0
  44. metadata +211 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2fcfdfe0d51cd70bcc1a5e49cd096ac4c6fb526d
4
+ data.tar.gz: 40330495883f655abadef5dcd7f8ab2afd4767b0
5
+ SHA512:
6
+ metadata.gz: 8cf18933b73c5370cb2e66aa7567a28c1e451266f4cc705cdbaa113de2434fa20f869fa48a6c0fce11f0111ed97559d6330c6451ff64b576af5d3f12c527d827
7
+ data.tar.gz: 54d0023132022e3179759039d122a71619e69669adeeffe7ae176a620c9501efdf7c43a5ee4e425ae8f2b7f1a046674a24537679791d35b827420414d0e48e90
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dynamo_record.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nguyen Vu Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # DynamoRecord
2
+
3
+ A simple DynamoDB ORM wrapper on top of aws-sdk v2
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dynamo_record'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dynamo_record
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/dynamo_record/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dynamo_record/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dynamo_record"
8
+ spec.version = DynamoRecord::VERSION
9
+ spec.authors = ["Nguyen Vu Nguyen"]
10
+ spec.email = ["nvunguyen@gmail.com"]
11
+ spec.summary = %q{A simple DynamoDB ORM wrapper on top of aws-sdk v2}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activesupport", "~> 4.1.0"
21
+ spec.add_dependency "aws-sdk", "~> 2.0.17.pre"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1.0"
26
+ spec.add_development_dependency "vcr", "~> 2.9.3"
27
+ spec.add_development_dependency "webmock", "~> 1.20.4"
28
+ end
@@ -0,0 +1,31 @@
1
+ module DynamoRecord
2
+ class Collection < Array
3
+
4
+ def initialize(pager = nil, klass = nil)
5
+ if pager && klass
6
+ @pager = pager
7
+ @klass = klass
8
+ replace @pager.items.map { |item| klass.send(:from_database, item) }
9
+ end
10
+ end
11
+
12
+ def next_page?
13
+ @pager ? @pager.next_page? : false
14
+ end
15
+
16
+ def last_page?
17
+ @pager ? @pager.last_page? : true
18
+ end
19
+
20
+ def next_page
21
+ return self.class.new(@pager.next_page, @klass) if @pager.next_page?
22
+ self.class.new
23
+ end
24
+
25
+ def each_page(&block)
26
+ @pager.each do |page|
27
+ yield self.class.new(page, @klass)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ module DynamoRecord
2
+ module Config
3
+ extend self
4
+
5
+ attr_accessor :access_key_id, :secret_access_key, :region, :namespace, :read_capacity_units, :write_capacity_units
6
+
7
+ def set_defaults
8
+ self.region = 'us-east-1'
9
+ self.read_capacity_units = 20
10
+ self.write_capacity_units = 20
11
+ self.namespace = nil
12
+ end
13
+ set_defaults
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module DynamoRecord
2
+ module Document
3
+ extend ActiveSupport::Concern
4
+ include DynamoRecord::Fields
5
+ include DynamoRecord::Persistence
6
+ include DynamoRecord::Finders
7
+ include DynamoRecord::Query
8
+
9
+ included do
10
+ class_attribute :base_class
11
+ self.base_class = self
12
+ end
13
+
14
+ module ClassMethods
15
+ def from_database(attrs)
16
+ self.new(attrs).tap { |r| r.new_record = false}
17
+ end
18
+ end
19
+
20
+ attr_accessor :new_record, :attributes
21
+
22
+ def initialize(attrs = {})
23
+ @new_record = true
24
+ @attributes = {}
25
+
26
+ attrs.each do |key, value|
27
+ send("#{key}=", value)
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ module DynamoRecord
2
+ module Fields
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :attributes
7
+
8
+ self.attributes = {}
9
+
10
+ field :id # default primary key
11
+ end
12
+
13
+ module ClassMethods
14
+ def field(name, type = :string, opts = {})
15
+ named = name.to_s
16
+ self.attributes = attributes.merge(name: {type: type, options: opts})
17
+
18
+ define_method("#{named}=") { |value| write_attribute(named, value) }
19
+ define_method("#{name}") { read_attribute(named) }
20
+ end
21
+ end
22
+
23
+ def write_attribute(name, value)
24
+ attributes[name.to_sym] = value
25
+ end
26
+
27
+ def read_attribute(name)
28
+ attributes[name.to_sym]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module DynamoRecord
2
+ module Finders
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def find(id)
7
+ response = client.get_item(
8
+ table_name: table_name,
9
+ key: {
10
+ id: id
11
+ }
12
+ )
13
+ response.item ? from_database(response.item) : new
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,85 @@
1
+ module DynamoRecord
2
+ module Persistence
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def table_name
7
+ @table_name ||= DynamoRecord::Config.namespace ? "#{DynamoRecord::Config.namespace}_#{base_class.name.downcase.pluralize}" : base_class.name.downcase.pluralize
8
+ end
9
+
10
+ def client
11
+ @client ||= Aws::DynamoDB::Client.new(
12
+ access_key_id: DynamoRecord::Config.access_key_id,
13
+ secret_access_key: DynamoRecord::Config.secret_access_key,
14
+ region: DynamoRecord::Config.region
15
+ )
16
+ end
17
+
18
+ def default_options
19
+ { table_name: self.table_name }
20
+ end
21
+
22
+ def create_table(opts = {})
23
+ table_name = opts[:table_name] || self.table_name
24
+ read_capacity = opts[:read_capacity] || DynamoRecord::Config.read_capacity_units
25
+ write_capacity = opts[:write_capacity] || DynamoRecord::Config.write_capacity_units
26
+
27
+ # Default id
28
+ attribute_definitions = [{attribute_name: "id", attribute_type: "S"}]
29
+
30
+ # Global indexes
31
+ indexes = []
32
+ attributes.each do |key, value|
33
+ indexes << key if value[:options][:index]
34
+ end
35
+
36
+ global_secondary_indexes = []
37
+ indexes.each do |index|
38
+ index_definition = {}
39
+ index_definition[:index_name] = "#{index}-index"
40
+ index_definition[:key_schema] = [{ attribute_name: index, key_type: 'HASH' }]
41
+ index_definition[:projection] = { projection_type: 'ALL'}
42
+ index_definition[:provisioned_throughput] = { read_capacity_units: read_capacity, write_capacity_units: write_capacity }
43
+ global_secondary_indexes << index_definition
44
+ attribute_definitions << {attribute_name: index.to_s,
45
+ attribute_type: attributes[index][:type] == :string ? 'S' : 'N'} # only String and Number are supported. Not Binary
46
+ end
47
+
48
+ options = {
49
+ attribute_definitions: attribute_definitions,
50
+ table_name: table_name,
51
+ key_schema: [
52
+ {
53
+ attribute_name: "id",
54
+ key_type: "HASH",
55
+ },
56
+ ],
57
+ provisioned_throughput: {
58
+ read_capacity_units: read_capacity,
59
+ write_capacity_units: write_capacity,
60
+ }
61
+ }
62
+
63
+ options.merge!(global_secondary_indexes: global_secondary_indexes) unless global_secondary_indexes.empty?
64
+
65
+ response = self.client.create_table(options)
66
+ end
67
+ end
68
+
69
+ def save
70
+ options = self.class.default_options
71
+ if id.nil?
72
+ self.id = SecureRandom.uuid
73
+ elsif @new_record # New item. Don't overwrite if id exists
74
+ options.merge!(condition_expression: 'id <> :s', expression_attribute_values: { ':s' => self.id })
75
+ end
76
+
77
+ options.merge!(item: attributes)
78
+ response = self.class.client.put_item(options)
79
+ @new_record = false
80
+ true
81
+ rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
82
+ false
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,29 @@
1
+ module DynamoRecord
2
+ module Query
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def all(opts = {})
7
+ options = self.default_options
8
+ options.merge!(opts.slice(:limit))
9
+
10
+ response = self.client.scan(options)
11
+ DynamoRecord::Collection.new(response, self)
12
+ end
13
+
14
+ def where(condition = {})
15
+ attribute = condition.first.first.to_s
16
+ value = condition.first.second
17
+ key_conditions = {}
18
+ key_conditions[attribute] = { attribute_value_list: [value],
19
+ comparison_operator: 'EQ' }
20
+ index_name = "#{attribute}-index"
21
+ options = self.default_options
22
+ options.merge!(key_conditions: key_conditions, index_name: index_name)
23
+
24
+ response = self.client.query(options)
25
+ DynamoRecord::Collection.new(response, self)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module DynamoRecord
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'securerandom'
2
+ require 'active_support/concern'
3
+ require 'active_support/core_ext'
4
+
5
+ require 'aws-sdk'
6
+
7
+ require 'dynamo_record/version'
8
+ require 'dynamo_record/config'
9
+ require 'dynamo_record/collection'
10
+ require 'dynamo_record/fields'
11
+ require 'dynamo_record/persistence'
12
+ require 'dynamo_record/finders'
13
+ require 'dynamo_record/query'
14
+ require 'dynamo_record/document'
15
+
16
+ module DynamoRecord
17
+ extend self
18
+
19
+ def configure
20
+ block_given? ? yield(DynamoRecord::Config) : DynamoRecord::Config
21
+ end
22
+ alias :config :configure
23
+
24
+ end
25
+
@@ -0,0 +1,5 @@
1
+ class Address
2
+ include DynamoRecord::Document
3
+
4
+ field :city, :string, index: true
5
+ end
@@ -0,0 +1,5 @@
1
+ class Person
2
+ include DynamoRecord::Document
3
+
4
+ field :name, :string
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DynamoRecord::Collection do
4
+
5
+ let(:pager) { spy('pager', :items => items, :'next_page?' => false, :'last_page?' => true) }
6
+ let(:items) {
7
+ [
8
+ { id: 'id1', name: 'Person 1' },
9
+ { id: 'id2', name: 'Person 2' }
10
+ ]
11
+ }
12
+
13
+ it 'initializes properly' do
14
+ collection = DynamoRecord::Collection.new(pager, Person)
15
+ expect(collection.count).to eq(2)
16
+ expect(collection.map(&:name)).to eq(['Person 1','Person 2'])
17
+ expect(collection.next_page?).to be_falsy
18
+ expect(collection.last_page?).to be_truthy
19
+ end
20
+
21
+ end