dynamoid 3.3.0 → 3.4.0

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
  SHA256:
3
- metadata.gz: eac484a25ff6886d838773be1b34f1fa9c7a51bf75ec46ed93984e97f273575c
4
- data.tar.gz: 349e23120b78776d352c576bd7ffe0d2d1a2b69b0b5107d81698435bcbe2fb2e
3
+ metadata.gz: 7dd36d27f502c400c3275bf584ce8eba095b32732db1b9567ad9a3ceac64eb50
4
+ data.tar.gz: '084f10baa66c9d22febfaa884f410be5222774283d58fda3f237c7b9b204c572'
5
5
  SHA512:
6
- metadata.gz: cdbe5b559ceea1a834c6bfdbd956bc0dcba57172be55a4003213dafdd54e702b8ae7c1f62f6a04c11f13f3f3907429cbff1e008ccfcef2b90e54b512c301b38f
7
- data.tar.gz: 796c4496a3cabdde4433ee18797176d674e8be5ca887978d73792bea7fdf8cc0e697ca475c5b50cc972217524a6a006751f7497c84dbbe40a0a296bad5b1a79d
6
+ metadata.gz: 43ac82ed7d651e4b92845bf08f372ca57bf7b3bde0c3f49a3afbc7b6cbd1125574938472d6041627882e82c90379e5063ecd285d0092761cb4a5c209f9041510
7
+ data.tar.gz: 2a7f71ed8470bfce21a0b1c761d99681f02bbd797be65b2a4fe21ea72c54b7c22e52571f43ddcbedf980ab24ac26a4b54df1c41fd464f2cc1bdd7ac10817df33
@@ -12,6 +12,29 @@
12
12
 
13
13
 
14
14
 
15
+ # 3.4.0
16
+
17
+ ## Features
18
+ * Feature: [#386](https://github.com/Dynamoid/dynamoid/pull/386) Disable timestamps fields on a table level with new
19
+ table option `timestamps`
20
+ * Feature: [#387](https://github.com/Dynamoid/dynamoid/pull/387) Add TTL support with table option `expires`
21
+ * Feature: [#393](https://github.com/Dynamoid/dynamoid/pull/393) Support pre-configured credentials with new config
22
+ option `credentials` (@emmajhyde)
23
+ * Feature: [#397](https://github.com/Dynamoid/dynamoid/pull/397) Configure on-demand table capacity mode with `capacity_mode` option
24
+
25
+ ## Improvements
26
+ * Improvement: [#388](https://github.com/Dynamoid/dynamoid/pull/388) Minor memory optimization - don't allocate excessive
27
+ hash (@arjes)
28
+
29
+ ## Fixes
30
+
31
+ Fix: [#382](https://github.com/Dynamoid/dynamoid/pull/382) Fixed deprecation warning about `Module#parent_name` in Rails 6 (@tmandke)
32
+ Fix: Typos in Readme.md (@romeuhcf)
33
+
34
+ ---
35
+
36
+
37
+
15
38
  # 3.3.0
16
39
 
17
40
  ## Features
data/README.md CHANGED
@@ -34,7 +34,7 @@ Gemfile:
34
34
  ```ruby
35
35
  gem 'dynamoid'
36
36
  ```
37
- ## Prerequisities
37
+ ## Prerequisites
38
38
 
39
39
  Dynamoid depends on the aws-sdk, and this is tested on the current
40
40
  version of aws-sdk (~> 3), rails (>= 4). Hence the configuration as
@@ -80,6 +80,27 @@ Dynamoid.configure do |config|
80
80
  end
81
81
  ```
82
82
 
83
+ Additionally, if you would like to pass in pre-configured AWS credentials
84
+ (e.g. you have an IAM role credential, you configure your credentials
85
+ elsewhere in your project, etc.), you may do so:
86
+
87
+ ```ruby
88
+ require 'dynamoid'
89
+
90
+ credentials = Aws::AssumeRoleCredentials.new(
91
+ region: region,
92
+ access_key_id: key,
93
+ secret_access_key: secret,
94
+ role_arn: role_arn,
95
+ role_session_name: 'our-session'
96
+ )
97
+
98
+ Dynamoid.configure do |config|
99
+ config.region = 'us-west-2',
100
+ config.credentials = credentials
101
+ end
102
+ ```
103
+
83
104
  For a full list of the DDB regions, you can go
84
105
  [here](http://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region).
85
106
 
@@ -143,6 +164,47 @@ won't change its hash key, which it expects will be `user_id`. If this
143
164
  table doesn't exist yet, however, Dynamoid will create it with these
144
165
  options.
145
166
 
167
+ There is a basic support of DynamoDB's [Time To Live (TTL)
168
+ mechanism](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html).
169
+ If you declare a field as TTL field - it will be initialised if doesn't
170
+ have value yet. Default value is current time + specified seconds.
171
+
172
+ ```ruby
173
+ class User
174
+ include Dynamoid::Document
175
+
176
+ table expires: { field: :ttl, after: 60 }
177
+
178
+ field :ttl, :integer
179
+ end
180
+ ```
181
+
182
+ Field used to store expiration time (e.g. `ttl`) should be declared
183
+ explicitly and should have numeric type (`integer`, `number`) only.
184
+ `datetime` type is also possible but only if it's stored as number
185
+ (there is a way to store time as a string also).
186
+
187
+ It's also possible to override a global option `Dynamoid::Config.timestamps`
188
+ on a table level:
189
+
190
+ ```ruby
191
+ table timestamps: false
192
+ ```
193
+
194
+ This option controls generation of timestamp fields
195
+ `created_at`/`updated_at`.
196
+
197
+ It's also possible to override table capacity mode configured globally
198
+ with table level option `capacity_mode`. Valid values are
199
+ `:provisioned`, `:on_demand` and `nil`:
200
+
201
+ ```ruby
202
+ table capacity_mode: :on_demand
203
+ ```
204
+
205
+ If table capacity mode is on-demand, another related table-level options
206
+ `read_capacity` and `write_capacity` will be ignored.
207
+
146
208
  ### Fields
147
209
 
148
210
  You'll have to define all the fields on the model and the data type of
@@ -165,8 +227,8 @@ backward-compatibility of the serialized representation.
165
227
 
166
228
  The boolean fields are stored as DynamoDB boolean values by default.
167
229
  Dynamoid can store boolean values as strings as well - `'t'` and `'f'`.
168
- So if you want to change default format of boolean field you can easily
169
- achieve this with `store_as_native_boolean` field option:
230
+ So if you want to change the default format of boolean field you can
231
+ easily achieve this with `store_as_native_boolean` field option:
170
232
 
171
233
  ```ruby
172
234
  class Document
@@ -513,7 +575,7 @@ c.my_new_type
513
575
 
514
576
  ### Type casting
515
577
 
516
- Dynamid supports type casting and tryes to do it in the most convinient
578
+ Dynamid supports type casting and tries to do it in the most convenient
517
579
  way. Values for all fields (except custom type) are coerced to declared
518
580
  field types.
519
581
 
@@ -551,7 +613,7 @@ well.
551
613
 
552
614
  ### Dirty API
553
615
 
554
- Dynamoid supports Dirty API which equvalents to [Rails 5.2
616
+ Dynamoid supports Dirty API which equivalents to [Rails 5.2
555
617
  `ActiveModel::Dirty`](https://api.rubyonrails.org/v5.2/classes/ActiveModel/Dirty.html).
556
618
  There is only one limitation - change in place of field isn't detected
557
619
  automatically.
@@ -624,7 +686,7 @@ to relational database engines.
624
686
 
625
687
  **WARNING:** There is a limitation of conditions passed to `where`
626
688
  method. Only one condition for some particular field could be specified.
627
- The last one only will be applyed and others will be ignored. E.g. in
689
+ The last one only will be applied and others will be ignored. E.g. in
628
690
  examples:
629
691
 
630
692
  ```ruby
@@ -714,7 +776,7 @@ commands.
714
776
  At times it can be useful to rely on DynamoDB [low-level
715
777
  pagination](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.Pagination)
716
778
  instead of fixed pages sizes. Each page results in a single Query or
717
- Scan call to DyanmoDB, but returns an unknown number of records.
779
+ Scan call to DynamoDB, but returns an unknown number of records.
718
780
 
719
781
  Access to the native DynamoDB pages can be obtained via the
720
782
  `find_by_pages` method, which yields arrays of records.
@@ -729,7 +791,7 @@ is a hash including a key `:last_evaluated_key`. The value of this key
729
791
  can be used for the `start` method to fetch the next page of records.
730
792
 
731
793
  This way it can be used for instance to implement efficiently pagination
732
- in web-application:
794
+ in web-applications:
733
795
 
734
796
  ```ruby
735
797
  class UserController < ApplicationController
@@ -774,7 +836,7 @@ Address.where('postcode.null': false)
774
836
  Address.where('postcode.not_null': true)
775
837
  ```
776
838
 
777
- **WARNING:** Please take into accout that `NULL` and `NOT_NULL`
839
+ **WARNING:** Please take into account that `NULL` and `NOT_NULL`
778
840
  operators check attribute presence in a document, not value. So if
779
841
  attribute `postcode`'s value is `NULL`, `NULL` operator will return
780
842
  false because attribute exists even if has `NULL` value.
@@ -831,7 +893,7 @@ User.where("created_at.lt": DateTime.now - 1.day).all
831
893
 
832
894
  It also supports `gte` and `lte`. Turning those into symbols and
833
895
  allowing a Rails SQL-style string syntax is in the works. You can only
834
- have one range argument per query, because of DynamoDB's inherent
896
+ have one range argument per query, because of DynamoDB inherent
835
897
  limitations, so use it sensibly!
836
898
 
837
899
 
@@ -839,7 +901,7 @@ limitations, so use it sensibly!
839
901
 
840
902
  In order to update document you can use high level methods
841
903
  `#update_attributes`, `#update_attribute` and `.update`. They run
842
- validation and collbacks.
904
+ validation and callbacks.
843
905
 
844
906
  ```ruby
845
907
  Address.find(id).update_attributes(city: 'Chicago')
@@ -855,12 +917,12 @@ it runs `update` callbacks). All of them support conditional updates.
855
917
  doesn't exist.
856
918
 
857
919
  ```ruby
858
- Adderess.find(id).update do |i|
920
+ Address.find(id).update do |i|
859
921
  i.set city: 'Chicago'
860
922
  i.add latitude: 100
861
923
  i.delete set_of_numbers: 10
862
924
  end
863
- Adderess.find(id).update(if: { deliverable: true }) do |i|
925
+ Address.find(id).update(if: { deliverable: true }) do |i|
864
926
  i.set city: 'Chicago'
865
927
  end
866
928
  Address.update_fields(id, city: 'Chicago')
@@ -872,7 +934,7 @@ Address.upsert(id, { city: 'Chicago' }, if: { deliverable: true })
872
934
  ### Deleting
873
935
 
874
936
  In order to delete some items `delete_all` method should be used. Any
875
- callback wont be called. Items delete in efficient way in batch.
937
+ callback won't be called. Items delete in efficient way in batch.
876
938
 
877
939
  ```ruby
878
940
  Address.where(city: 'London').delete_all
@@ -893,17 +955,17 @@ class User
893
955
  end
894
956
  ```
895
957
 
896
- There are following options:
958
+ There are the following options:
897
959
  * `hash_key` - is used as hash key of an index,
898
960
  * `range_key` - is used as range key of an index,
899
961
  * `projected_attributes` - list of fields to store in an index or has a
900
- predefiled value `:keys_only`, `:all`; `:keys_only` is a default,
962
+ predefined value `:keys_only`, `:all`; `:keys_only` is a default,
901
963
  * `name` - an index will be created with this name when a table is
902
964
  created; by default name is generated and contains table name and keys
903
965
  names,
904
- * `read_capacity` - is used when table creates and used as an index
966
+ * `read_capacity` - is used when table created and used as an index
905
967
  capacity; by default equals `Dynamoid::Config.read_capacity`,
906
- * `write_capacity` - is used when table creates and used as an index
968
+ * `write_capacity` - is used when table created and used as an index
907
969
  capacity; by default equals `Dynamoid::Config.write_capacity`
908
970
 
909
971
  The only mandatory option is `name`.
@@ -954,7 +1016,7 @@ on the base table*
954
1016
 
955
1017
  Listed below are all configuration options.
956
1018
 
957
- * `adapter` - usefull only for the gem developers to switch to a new
1019
+ * `adapter` - useful only for the gem developers to switch to a new
958
1020
  adapter. Default and the only available value is `aws_sdk_v3`
959
1021
  * `namespace` - prefix for table names, default is
960
1022
  `dynamoid_#{application_name}_#{environment}` for Rails application
@@ -962,15 +1024,21 @@ Listed below are all configuration options.
962
1024
  * `logger` - by default it's a `Rails.logger` in Rails application and
963
1025
  `stdout` otherwise. You can disable logging by setting `nil` or
964
1026
  `false` values. Set `true` value to use defaults
965
- * `access_key` - DynamoDb custom credentials for AWS, override global
966
- AWS credentials if they present
967
- * `secret_key` - DynamoDb custom credentials for AWS, override global
968
- AWS credentials if they present
1027
+ * `access_key` - DynamoDb custom access key for AWS credentials, override global
1028
+ AWS credentials if they're present
1029
+ * `secret_key` - DynamoDb custom secret key for AWS credentials, override global
1030
+ AWS credentials if they're present
1031
+ * `credentials` - DynamoDb custom pre-configured credentials, override global
1032
+ AWS credentials if they're present
969
1033
  * `region` - DynamoDb custom credentials for AWS, override global AWS
970
- credentials if they present
1034
+ credentials if they're present
971
1035
  * `batch_size` - when you try to load multiple items at once with
972
1036
  * `batch_get_item` call Dynamoid loads them not with one api call but
973
1037
  piece by piece. Default is 100 items
1038
+ * `capacity_mode` - used at a table creation and means whether a table
1039
+ read/write capacity mode will be on-demand or provisioned. Allowed
1040
+ values are `:on_demand` and `:provisioned`. Default value is `nil` which
1041
+ means provisioned mode will be used.
974
1042
  * `read_capacity` - is used at table or indices creation. Default is 100
975
1043
  (units)
976
1044
  * `write_capacity` - is used at table or indices creation. Default is 20
@@ -984,7 +1052,7 @@ Listed below are all configuration options.
984
1052
  when referring to them. Isn't thread safe. Default is `false`.
985
1053
  `Use Dynamoid::Middleware::IdentityMap` to clear identity map for each HTTP request
986
1054
  * `timestamps` - by default Dynamoid sets `created_at` and `updated_at`
987
- fields for model at creation and updating. You can disable this
1055
+ fields for model creation and updating. You can disable this
988
1056
  behavior by setting `false` value
989
1057
  * `sync_retry_max_times` - when Dynamoid creates or deletes table
990
1058
  synchronously it checks for completion specified times. Default is 60
@@ -1024,7 +1092,7 @@ Listed below are all configuration options.
1024
1092
  100-continue HTTP response before sending the request body. Default
1025
1093
  option value is `nil`. If not specified effected value is `1`
1026
1094
  * `http_idle_timeout`: The number of seconds an HTTP connection is
1027
- allowed to sit idble before it is considered stale. Default option
1095
+ allowed to sit idle before it is considered stale. Default option
1028
1096
  value is `nil`. If not specified effected value is `5`
1029
1097
  * `http_open_timeout`: The number of seconds to wait when opening a HTTP
1030
1098
  session. Default option value is `nil`. If not specified effected
@@ -1056,7 +1124,7 @@ reload the row (so that it will pick up the newest values), and try the
1056
1124
  save again.
1057
1125
 
1058
1126
  Calls to `update` and `update!` also increment the `lock_version`,
1059
- however they do not check the existing value. This guarantees that a
1127
+ however, they do not check the existing value. This guarantees that a
1060
1128
  update operation will raise an exception in a concurrent save operation,
1061
1129
  however a save operation will never cause an update to fail. Thus,
1062
1130
  `update` is useful & safe only for doing atomic operations (e.g.
@@ -1102,7 +1170,7 @@ Dynamoid.configure do |config|
1102
1170
  end
1103
1171
  ```
1104
1172
 
1105
- You can use your own strategy in following way:
1173
+ You can use your own strategy in the following way:
1106
1174
 
1107
1175
  ```ruby
1108
1176
  Dynamoid.configure do |config|
@@ -124,8 +124,12 @@ module Dynamoid
124
124
 
125
125
  def create_table(table_name, key, options = {})
126
126
  unless tables.include?(table_name)
127
- benchmark('Create Table') { adapter.create_table(table_name, key, options) }
127
+ result = nil
128
+ benchmark('Create Table') { result = adapter.create_table(table_name, key, options) }
128
129
  tables << table_name
130
+ result
131
+ else
132
+ false
129
133
  end
130
134
  end
131
135
 
@@ -75,11 +75,18 @@ module Dynamoid
75
75
  (Dynamoid::Config.settings.compact.keys & CONNECTION_CONFIG_OPTIONS).each do |option|
76
76
  @connection_hash[option] = Dynamoid::Config.send(option)
77
77
  end
78
- if Dynamoid::Config.access_key?
79
- @connection_hash[:access_key_id] = Dynamoid::Config.access_key
80
- end
81
- if Dynamoid::Config.secret_key?
82
- @connection_hash[:secret_access_key] = Dynamoid::Config.secret_key
78
+
79
+ # if credentials are passed, they already contain access key & secret key
80
+ if Dynamoid::Config.credentials?
81
+ connection_hash[:credentials] = Dynamoid::Config.credentials
82
+ else
83
+ # otherwise, pass access key & secret key for credentials creation
84
+ if Dynamoid::Config.access_key?
85
+ connection_hash[:access_key_id] = Dynamoid::Config.access_key
86
+ end
87
+ if Dynamoid::Config.secret_key?
88
+ connection_hash[:secret_access_key] = Dynamoid::Config.secret_key
89
+ end
83
90
  end
84
91
 
85
92
  # https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-core/lib/aws-sdk-core/plugins/logging.rb
@@ -246,8 +253,22 @@ module Dynamoid
246
253
  def create_table(table_name, key = :id, options = {})
247
254
  Dynamoid.logger.info "Creating #{table_name} table. This could take a while."
248
255
  CreateTable.new(client, table_name, key, options).call
256
+ true
249
257
  rescue Aws::DynamoDB::Errors::ResourceInUseException => e
250
258
  Dynamoid.logger.error "Table #{table_name} cannot be created as it already exists"
259
+ false
260
+ end
261
+
262
+ def update_time_to_live(table_name:, attribute:)
263
+ request = {
264
+ table_name: table_name,
265
+ time_to_live_specification: {
266
+ attribute_name: attribute,
267
+ enabled: true,
268
+ }
269
+ }
270
+
271
+ client.update_time_to_live(request)
251
272
  end
252
273
 
253
274
  # Create a table on DynamoDB *synchronously*.
@@ -16,6 +16,7 @@ module Dynamoid
16
16
  end
17
17
 
18
18
  def call
19
+ billing_mode = options[:billing_mode]
19
20
  read_capacity = options[:read_capacity] || Dynamoid::Config.read_capacity
20
21
  write_capacity = options[:write_capacity] || Dynamoid::Config.write_capacity
21
22
 
@@ -41,14 +42,20 @@ module Dynamoid
41
42
 
42
43
  client_opts = {
43
44
  table_name: table_name,
44
- provisioned_throughput: {
45
- read_capacity_units: read_capacity,
46
- write_capacity_units: write_capacity
47
- },
48
45
  key_schema: key_schema,
49
46
  attribute_definitions: attribute_definitions
50
47
  }
51
48
 
49
+ if billing_mode == :on_demand
50
+ client_opts[:billing_mode] = 'PAY_PER_REQUEST'
51
+ else
52
+ client_opts[:billing_mode] = 'PROVISIONED'
53
+ client_opts[:provisioned_throughput] = {
54
+ read_capacity_units: read_capacity,
55
+ write_capacity_units: write_capacity
56
+ }
57
+ end
58
+
52
59
  if ls_indexes.present?
53
60
  client_opts[:local_secondary_indexes] = ls_indexes.map do |index|
54
61
  index_to_aws_hash(index)
@@ -14,6 +14,7 @@ module Dynamoid
14
14
 
15
15
  before_create :set_created_at
16
16
  before_save :set_updated_at
17
+ before_save :set_expires_field
17
18
  after_initialize :set_inheritance_field
18
19
  end
19
20
 
@@ -10,6 +10,15 @@ require 'dynamoid/config/backoff_strategies/exponential_backoff'
10
10
  module Dynamoid
11
11
  # Contains all the basic configuration information required for Dynamoid: both sensible defaults and required fields.
12
12
  module Config
13
+ # @since 3.3.1
14
+ DEFAULT_NAMESPACE = if defined?(Rails)
15
+ klass = Rails.application.class
16
+ app_name = Rails::VERSION::MAJOR >= 6 ? klass.module_parent_name : klass.parent_name
17
+ "dynamoid_#{app_name}_#{Rails.env}".freeze
18
+ else
19
+ 'dynamoid'.freeze
20
+ end
21
+
13
22
  extend self
14
23
 
15
24
  extend Options
@@ -17,11 +26,13 @@ module Dynamoid
17
26
 
18
27
  # All the default options.
19
28
  option :adapter, default: 'aws_sdk_v3'
20
- option :namespace, default: defined?(Rails) ? "dynamoid_#{Rails.application.class.parent_name}_#{Rails.env}" : 'dynamoid'
29
+ option :namespace, default: DEFAULT_NAMESPACE
21
30
  option :access_key, default: nil
22
31
  option :secret_key, default: nil
32
+ option :credentials, default: nil
23
33
  option :region, default: nil
24
34
  option :batch_size, default: 100
35
+ option :capacity_mode, default: nil
25
36
  option :read_capacity, default: 100
26
37
  option :write_capacity, default: 20
27
38
  option :warn_on_scan, default: true
@@ -84,5 +95,6 @@ module Dynamoid
84
95
  backoff_strategies[backoff].call
85
96
  end
86
97
  end
98
+
87
99
  end
88
100
  end
@@ -51,6 +51,13 @@ module Dynamoid #:nodoc:
51
51
  options[:write_capacity] || Dynamoid::Config.write_capacity
52
52
  end
53
53
 
54
+
55
+ # Returns the billing (capacity) mode for this table.
56
+ # Could be either :provisioned or :on_demand
57
+ def capacity_mode
58
+ options[:capacity_mode] || Dynamoid::Config.capacity_mode
59
+ end
60
+
54
61
  # Returns the field name used to support STI for this table.
55
62
  def inheritance_field
56
63
  options[:inheritance_field] || :type
@@ -138,13 +145,11 @@ module Dynamoid #:nodoc:
138
145
  @associations ||= {}
139
146
  @attributes_before_type_cast ||= {}
140
147
 
141
- attrs_with_defaults = self.class.attributes.reduce({}) do |res, (attribute, options)|
148
+ attrs_with_defaults = self.class.attributes.each_with_object({}) do |(attribute, options), res|
142
149
  if attrs.key?(attribute)
143
- res.merge(attribute => attrs[attribute])
150
+ res[attribute] = attrs[attribute]
144
151
  elsif options.key?(:default)
145
- res.merge(attribute => evaluate_default_value(options[:default]))
146
- else
147
- res
152
+ res[attribute] = evaluate_default_value(options[:default])
148
153
  end
149
154
  end
150
155
 
@@ -21,8 +21,11 @@ module Dynamoid #:nodoc:
21
21
  class_attribute :range_key
22
22
 
23
23
  self.attributes = {}
24
- field :created_at, :datetime
25
- field :updated_at, :datetime
24
+
25
+ # Timestamp fields could be disabled later in `table` method call.
26
+ # So let's declare them here and remove them later if it will be necessary
27
+ field :created_at, :datetime if Dynamoid::Config.timestamps
28
+ field :updated_at, :datetime if Dynamoid::Config.timestamps
26
29
 
27
30
  field :id # Default primary key
28
31
  end
@@ -75,12 +78,24 @@ module Dynamoid #:nodoc:
75
78
  self.range_key = name
76
79
  end
77
80
 
78
- def table(_options)
81
+ def table(options)
79
82
  # a default 'id' column is created when Dynamoid::Document is included
80
83
  unless attributes.key? hash_key
81
84
  remove_field :id
82
85
  field(hash_key)
83
86
  end
87
+
88
+ if options[:timestamps] && !Dynamoid::Config.timestamps
89
+ # Timestamp fields weren't declared in `included` hook because they
90
+ # are disabled globaly
91
+ field :created_at, :datetime
92
+ field :updated_at, :datetime
93
+ elsif options[:timestamps] == false && Dynamoid::Config.timestamps
94
+ # Timestamp fields were declared in `included` hook but they are
95
+ # disabled for a table
96
+ remove_field :created_at
97
+ remove_field :updated_at
98
+ end
84
99
  end
85
100
 
86
101
  def remove_field(field)
@@ -99,6 +114,10 @@ module Dynamoid #:nodoc:
99
114
  end
100
115
  end
101
116
 
117
+ def timestamps_enabled?
118
+ options[:timestamps] || (options[:timestamps].nil? && Dynamoid::Config.timestamps)
119
+ end
120
+
102
121
  private
103
122
 
104
123
  def generated_methods
@@ -166,18 +185,31 @@ module Dynamoid #:nodoc:
166
185
  #
167
186
  # @since 0.2.0
168
187
  def set_created_at
169
- self.created_at ||= DateTime.now.in_time_zone(Time.zone) if Dynamoid::Config.timestamps
188
+ self.created_at ||= DateTime.now.in_time_zone(Time.zone) if self.class.timestamps_enabled?
170
189
  end
171
190
 
172
191
  # Automatically called during the save callback to set the updated_at time.
173
192
  #
174
193
  # @since 0.2.0
175
194
  def set_updated_at
176
- if Dynamoid::Config.timestamps && !updated_at_changed?
195
+ if self.class.timestamps_enabled? && !updated_at_changed?
177
196
  self.updated_at = DateTime.now.in_time_zone(Time.zone)
178
197
  end
179
198
  end
180
199
 
200
+ def set_expires_field
201
+ options = self.class.options[:expires]
202
+
203
+ if options.present?
204
+ name = options[:field]
205
+ seconds = options[:after]
206
+
207
+ if self[name].blank?
208
+ send("#{name}=", Time.now.to_i + seconds)
209
+ end
210
+ end
211
+ end
212
+
181
213
  def set_inheritance_field
182
214
  # actually it does only following logic:
183
215
  # self.type ||= self.class.name if self.class.attributes[:type]
@@ -46,6 +46,7 @@ module Dynamoid
46
46
  options = {
47
47
  id: hash_key,
48
48
  table_name: table_name,
49
+ billing_mode: capacity_mode,
49
50
  write_capacity: write_capacity,
50
51
  read_capacity: read_capacity,
51
52
  range_key: range_key_hash,
@@ -54,7 +55,12 @@ module Dynamoid
54
55
  global_secondary_indexes: global_secondary_indexes.values
55
56
  }.merge(options)
56
57
 
57
- Dynamoid.adapter.create_table(options[:table_name], options[:id], options)
58
+ created_successfuly = Dynamoid.adapter.create_table(options[:table_name], options[:id], options)
59
+
60
+ if created_successfuly && self.options[:expires]
61
+ attribute = self.options[:expires][:field]
62
+ Dynamoid.adapter.update_time_to_live(table_name: table_name, attribute: attribute)
63
+ end
58
64
  end
59
65
 
60
66
  # Deletes the table for the model
@@ -32,7 +32,7 @@ module Dynamoid
32
32
  def build_model(attributes)
33
33
  attrs = attributes.symbolize_keys
34
34
 
35
- if Dynamoid::Config.timestamps
35
+ if @model_class.timestamps_enabled?
36
36
  time_now = DateTime.now.in_time_zone(Time.zone)
37
37
  attrs[:created_at] ||= time_now
38
38
  attrs[:updated_at] ||= time_now
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dynamoid
4
- VERSION = '3.3.0'
4
+ VERSION = '3.4.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Symonds
@@ -21,7 +21,7 @@ authors:
21
21
  autorequire:
22
22
  bindir: exe
23
23
  cert_chain: []
24
- date: 2019-08-20 00:00:00.000000000 Z
24
+ date: 2019-11-30 00:00:00.000000000 Z
25
25
  dependencies:
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activemodel
@@ -315,8 +315,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
315
315
  - !ruby/object:Gem::Version
316
316
  version: '0'
317
317
  requirements: []
318
- rubyforge_project:
319
- rubygems_version: 2.7.6
318
+ rubygems_version: 3.0.3
320
319
  signing_key:
321
320
  specification_version: 4
322
321
  summary: Dynamoid is an ORM for Amazon's DynamoDB