activeitem 0.0.25 → 0.0.26
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/CHANGELOG.md +20 -0
- data/lib/active_item/associations.rb +27 -10
- data/lib/active_item/base.rb +16 -2
- data/lib/active_item/database_helpers.rb +5 -2
- data/lib/active_item/errors.rb +5 -0
- data/lib/active_item/query_helpers.rb +7 -2
- data/lib/active_item/relation.rb +179 -38
- data/lib/active_item/transaction.rb +4 -1
- data/lib/active_item/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cbaab4f038aa32c3a5680afa9adfa01cc4fd8a408210fe8abc2e82d414ef7215
|
|
4
|
+
data.tar.gz: 42434a3dcb2d806924e9dddcc21a8f4bf576b07b9a5dc8791e16414fe10fa796
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca84f380d89e04a0bf6e5450b30bcbb7b8a0fcae47c867db9b6a7339fc6d5b5ae3d9abdc80e49ee9006869b6dcbd7443e1dc1bbc1f874726673d8c07295dbc14
|
|
7
|
+
data.tar.gz: b6325bf77e8f99ce2f2ecf4ba92a86ff592d828d1b920a6b4d9af546f743d9ffd28cf78011a192224905f527561f67bfd2162b85affabdc61845ab1072faf82a
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.0.26
|
|
4
|
+
|
|
5
|
+
Scope hardening from the Posh Nosh security review (Fizzy #2004). Several of these change behavior; see **Changed**.
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- **`accepts_nested_attributes_for` could update or destroy any child by id.** Nested rows were looked up with an unscoped `find`, so a parent could edit or `_destroy` another parent's child. A row whose foreign key isn't this parent's now raises `ActiveItem::RecordNotFound`, and the foreign key can no longer be reassigned through nested attributes (it is stripped and re-asserted on update and create).
|
|
10
|
+
- **A mass-assigned `id` redirected an update to another row.** `record.update(params)` with an `id` in the hash wrote the changes onto that other row. Changing the primary key of a persisted record now raises `ActiveItem::ReadonlyAttributeError`; assigning the same id back (form round-trips) is still allowed.
|
|
11
|
+
- **`relation.find(id)` only checked the first `where` condition.** Every condition and `where.not` condition is now checked. Conditions that can't be evaluated in Ruby with DynamoDB's semantics (ranges, nested paths) are checked by a filtered query instead. Preloaded (`includes`) and `has_many :through` relations, and `where(id: [...])`, find only among their loaded records. `none.find` and a nil-key association's `find` raise instead of returning any row. Preloaded `has_many` relations also keep the owner condition, so `where`/`exists?`/`reload` on them stay scoped.
|
|
12
|
+
- **`ilike: true` loosened every string condition in the relation.** `where(tenant_id: t).where(name: q, ilike: true)` substring-matched `tenant_id` too. ilike now applies per attribute.
|
|
13
|
+
- **Creating inside `Model.transaction` silently replaced an existing row.** The transactional put now carries the same `attribute_not_exists` condition as a normal create.
|
|
14
|
+
- **Filtered lookups on unindexed attributes made one Scan per item.** `find_by`, `exists?`, `limit`, `count` and uniqueness validation sent the wanted result count as DynamoDB's `Limit`, which applies *before* the filter. `Limit` is now only sent when there is no filter (scan, query, count, fan-out, pagination and `explain`); results are still trimmed to the limit.
|
|
15
|
+
- Paginated GSI queries could skip items when a page was trimmed; the next cursor now always resumes after the last returned item.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- `where(..., ilike: :name)` / `ilike: [:name, :sku]` names the attributes to match case-insensitively. `ilike: true` still works when that `where` call has exactly one string condition, and raises `ArgumentError` when it has several. `Relation#ilike` is now a boolean; the attribute list is `Relation#ilike_attributes`.
|
|
20
|
+
- A transactional create on an existing key now fails the whole transaction with `ActiveItem::TransactionError` at commit, instead of overwriting. Explicit `txn.put(record)` is unchanged (still an upsert); it now also accepts `expression_attribute_names:` alongside `condition:`.
|
|
21
|
+
- Nested attributes naming another parent's child raise `ActiveItem::RecordNotFound` from `save`.
|
|
22
|
+
|
|
3
23
|
## 0.0.25
|
|
4
24
|
|
|
5
25
|
### Fixed
|
|
@@ -266,22 +266,35 @@ module ActiveItem
|
|
|
266
266
|
next unless assoc_config
|
|
267
267
|
|
|
268
268
|
target_class = safe_constantize_model(assoc_config[:class_name])
|
|
269
|
-
foreign_key = assoc_config[:foreign_key]
|
|
269
|
+
foreign_key = assoc_config[:foreign_key].to_s
|
|
270
|
+
owner_key = send(assoc_config[:primary_key])
|
|
271
|
+
|
|
272
|
+
# Only children that already belong to this parent can be updated or destroyed
|
|
273
|
+
find_child = lambda do |child_id|
|
|
274
|
+
child = target_class.find(child_id)
|
|
275
|
+
unless child.send(foreign_key) == owner_key
|
|
276
|
+
raise ActiveItem::RecordNotFound,
|
|
277
|
+
"Couldn't find #{target_class.name} with id=#{child_id} for #{self.class.name} with #{assoc_config[:primary_key]}=#{owner_key}"
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
child
|
|
281
|
+
end
|
|
270
282
|
|
|
271
283
|
config[:records].each do |attrs|
|
|
272
284
|
attrs = attrs.transform_keys(&:to_sym)
|
|
273
285
|
|
|
274
286
|
if attrs[:_destroy] && config[:allow_destroy]
|
|
275
|
-
if attrs[:id]
|
|
276
|
-
record = target_class.find(attrs[:id])
|
|
277
|
-
record.destroy!
|
|
278
|
-
end
|
|
287
|
+
find_child.call(attrs[:id]).destroy! if attrs[:id]
|
|
279
288
|
elsif attrs[:id]
|
|
280
|
-
record =
|
|
281
|
-
record.assign_attributes(attrs.except(:id, :_destroy))
|
|
289
|
+
record = find_child.call(attrs[:id])
|
|
290
|
+
record.assign_attributes(attrs.except(:id, :_destroy, foreign_key.to_sym))
|
|
291
|
+
# Re-assert the owner: an alias writer (e.g. <association>_id=) could have moved it
|
|
292
|
+
record.send("#{foreign_key}=", owner_key)
|
|
282
293
|
record.save!
|
|
283
294
|
else
|
|
284
|
-
target_class.
|
|
295
|
+
record = target_class.new(attrs.except(:_destroy))
|
|
296
|
+
record.send("#{foreign_key}=", owner_key)
|
|
297
|
+
record.save!
|
|
285
298
|
end
|
|
286
299
|
end
|
|
287
300
|
end
|
|
@@ -302,12 +315,16 @@ module ActiveItem
|
|
|
302
315
|
config = self.class._associations[name]
|
|
303
316
|
return Relation.new(Object, conditions: { _empty: true }) unless config
|
|
304
317
|
|
|
305
|
-
return Relation.new(nil, preloaded_records: _preloaded_associations[name], class_name: config[:class_name]) if _preloaded_associations.key?(name)
|
|
306
|
-
|
|
307
318
|
local_key_value = send(config[:primary_key])
|
|
308
319
|
return Relation.new(nil, conditions: { _empty: true }, class_name: config[:class_name]) if local_key_value.nil?
|
|
309
320
|
|
|
310
321
|
conditions = { config[:foreign_key].to_sym => local_key_value }
|
|
322
|
+
|
|
323
|
+
# Preloaded records keep the owner condition, so where/reload/build on them stay scoped
|
|
324
|
+
if _preloaded_associations.key?(name)
|
|
325
|
+
return Relation.new(nil, preloaded_records: _preloaded_associations[name], conditions: conditions,
|
|
326
|
+
index_name: config[:index], class_name: config[:class_name], owner: self)
|
|
327
|
+
end
|
|
311
328
|
relation = Relation.new(nil, conditions: conditions, index_name: config[:index],
|
|
312
329
|
class_name: config[:class_name], owner: self)
|
|
313
330
|
|
data/lib/active_item/base.rb
CHANGED
|
@@ -35,7 +35,15 @@ module ActiveItem
|
|
|
35
35
|
attr_accessor :created_at, :updated_at, :dbrecord
|
|
36
36
|
|
|
37
37
|
def id=(value)
|
|
38
|
-
|
|
38
|
+
value = nil if value.to_s.strip.empty?
|
|
39
|
+
# Re-assigning the same id (forms send it back) is fine; changing it is not.
|
|
40
|
+
if persisted?
|
|
41
|
+
return if value.to_s == @id.to_s
|
|
42
|
+
|
|
43
|
+
raise ReadonlyAttributeError, "#{self.class.primary_key} can't be changed on a persisted #{self.class.name}"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
@id = value
|
|
39
47
|
end
|
|
40
48
|
|
|
41
49
|
before_create :generate_primary_key
|
|
@@ -169,6 +177,8 @@ module ActiveItem
|
|
|
169
177
|
def to_dynamo_key(attr_name)
|
|
170
178
|
attr_str = attr_name.to_s
|
|
171
179
|
return dynamo_attribute_map[attr_str] if dynamo_attribute_map.key?(attr_str)
|
|
180
|
+
# The primary key is stored under its own name (see build_dynamodb_item)
|
|
181
|
+
return attr_str if attr_str == primary_key.to_s
|
|
172
182
|
|
|
173
183
|
attr_str.camelize(:lower)
|
|
174
184
|
end
|
|
@@ -592,7 +602,11 @@ module ActiveItem
|
|
|
592
602
|
|
|
593
603
|
result = run_callbacks :save do
|
|
594
604
|
if new_record?
|
|
595
|
-
|
|
605
|
+
# Same guard as perform_create: a create must not replace an existing row
|
|
606
|
+
run_callbacks(:create) do
|
|
607
|
+
txn.put(self, condition: 'attribute_not_exists(#pk)',
|
|
608
|
+
expression_attribute_names: { '#pk' => self.class.primary_key.to_s })
|
|
609
|
+
end
|
|
596
610
|
else
|
|
597
611
|
run_callbacks(:update) { txn.update(self) }
|
|
598
612
|
end
|
|
@@ -42,7 +42,10 @@ module ActiveItem
|
|
|
42
42
|
|
|
43
43
|
def scan(limit: nil, filter_expression: nil, expression_attribute_names: nil, expression_attribute_values: nil, projection_expression: nil)
|
|
44
44
|
params = { table_name: table_name }
|
|
45
|
-
|
|
45
|
+
# DynamoDB applies Limit before the filter, so with a filter it caps items
|
|
46
|
+
# *read* per page, not matches; a small limit would then cost one request
|
|
47
|
+
# per item in the table. Only send it when there is no filter.
|
|
48
|
+
params[:limit] = limit if limit && !filter_expression
|
|
46
49
|
params[:filter_expression] = filter_expression if filter_expression
|
|
47
50
|
params[:expression_attribute_names] = expression_attribute_names if expression_attribute_names
|
|
48
51
|
params[:expression_attribute_values] = expression_attribute_values if expression_attribute_values
|
|
@@ -69,7 +72,7 @@ module ActiveItem
|
|
|
69
72
|
break if limit && items.length >= limit
|
|
70
73
|
end
|
|
71
74
|
|
|
72
|
-
items
|
|
75
|
+
limit ? items.first(limit) : items
|
|
73
76
|
rescue Aws::DynamoDB::Errors::AccessDeniedException => e
|
|
74
77
|
raise_access_denied('Scan', e)
|
|
75
78
|
end
|
data/lib/active_item/errors.rb
CHANGED
|
@@ -4,6 +4,11 @@ module ActiveItem
|
|
|
4
4
|
class RecordNotFound < StandardError; end
|
|
5
5
|
class TransactionError < StandardError; end
|
|
6
6
|
|
|
7
|
+
# Raised when assigning a different primary key to a persisted record.
|
|
8
|
+
# Writes are keyed on the id, so a mass-assigned id (record.update(params))
|
|
9
|
+
# would otherwise redirect the update to another row.
|
|
10
|
+
class ReadonlyAttributeError < StandardError; end
|
|
11
|
+
|
|
7
12
|
# Raised by save! when validations fail.
|
|
8
13
|
class RecordInvalid < StandardError
|
|
9
14
|
attr_reader :record
|
|
@@ -166,6 +166,10 @@ module ActiveItem
|
|
|
166
166
|
# @example Case-insensitive search (ilike option)
|
|
167
167
|
# Container.where(name: 'box', ilike: true) # Substring match
|
|
168
168
|
# Container.where(name: 'box', ilike: true, exact: true) # Exact case-insensitive
|
|
169
|
+
# Container.where(owner_id: id, name: 'box', ilike: :name) # Only name; owner_id stays exact
|
|
170
|
+
#
|
|
171
|
+
# ilike applies per attribute. ilike: true covers the single string condition in
|
|
172
|
+
# that where call and raises ArgumentError if there are several; name them instead.
|
|
169
173
|
#
|
|
170
174
|
# @example Batch find by primary key (automatically uses BatchGetItem)
|
|
171
175
|
# Customer.where(customer_id: ['cust-1', 'cust-2', 'cust-3'])
|
|
@@ -179,7 +183,7 @@ module ActiveItem
|
|
|
179
183
|
|
|
180
184
|
# Extract special options
|
|
181
185
|
index_name = conditions.delete(:index) || conditions.delete(:index_name)
|
|
182
|
-
ilike = conditions.delete(:ilike)
|
|
186
|
+
ilike = conditions.delete(:ilike)
|
|
183
187
|
exact = conditions.delete(:exact) || false
|
|
184
188
|
|
|
185
189
|
# Optimization: Detect primary key array queries and use batch_find
|
|
@@ -200,7 +204,8 @@ module ActiveItem
|
|
|
200
204
|
end
|
|
201
205
|
end
|
|
202
206
|
|
|
203
|
-
Relation.new(self, conditions: conditions, index_name: index_name,
|
|
207
|
+
Relation.new(self, conditions: conditions, index_name: index_name,
|
|
208
|
+
ilike_attributes: Relation.resolve_ilike_attributes(ilike, conditions), ilike_exact: exact)
|
|
204
209
|
end
|
|
205
210
|
|
|
206
211
|
# Returns a Relation for all records (enables chaining from .all)
|
data/lib/active_item/relation.rb
CHANGED
|
@@ -10,11 +10,40 @@ module ActiveItem
|
|
|
10
10
|
include Enumerable
|
|
11
11
|
include ModelLoader
|
|
12
12
|
|
|
13
|
-
attr_reader :model, :conditions, :index_name, :limit_value, :not_conditions, :
|
|
14
|
-
:order_direction, :select_attributes
|
|
13
|
+
attr_reader :model, :conditions, :index_name, :limit_value, :not_conditions, :ilike_attributes, :ilike_exact, :class_name, :owner,
|
|
14
|
+
:includes_associations, :order_direction, :select_attributes
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
# Resolve a where call's `ilike:` option into the attribute names it applies to.
|
|
17
|
+
#
|
|
18
|
+
# ilike is per attribute so it never loosens a scope condition passed
|
|
19
|
+
# alongside it (where(tenant_id: t, name: q, ilike: true) must keep
|
|
20
|
+
# tenant_id exact).
|
|
21
|
+
#
|
|
22
|
+
# ilike: :name / ilike: [:name, :sku] -> those attributes
|
|
23
|
+
# ilike: true -> the one String condition in this call;
|
|
24
|
+
# raises if there is more than one
|
|
25
|
+
# ilike: nil / false -> none
|
|
26
|
+
def self.resolve_ilike_attributes(option, conditions)
|
|
27
|
+
return [] if option.nil? || option == false
|
|
28
|
+
|
|
29
|
+
if option == true
|
|
30
|
+
names = conditions.select { |_, v| v.is_a?(String) }.keys.map(&:to_sym)
|
|
31
|
+
if names.size > 1
|
|
32
|
+
raise ArgumentError, "ilike: true is ambiguous with several string conditions (#{names.join(', ')}); " \
|
|
33
|
+
"name the attribute instead, e.g. ilike: :#{names.last}"
|
|
34
|
+
end
|
|
35
|
+
return names
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
names = Array(option).map(&:to_sym)
|
|
39
|
+
unknown = names - conditions.keys.map(&:to_sym)
|
|
40
|
+
raise ArgumentError, "ilike names #{unknown.join(', ')}, which #{unknown.size == 1 ? 'is' : 'are'} not a condition in this where" if unknown.any?
|
|
41
|
+
|
|
42
|
+
names
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def initialize(model, conditions: {}, index_name: nil, limit_value: nil, not_conditions: {}, ilike_attributes: [], ilike_exact: false,
|
|
46
|
+
class_name: nil, owner: nil, preloaded_records: nil, includes_associations: [], order_direction: nil, select_attributes: nil)
|
|
18
47
|
@model = model
|
|
19
48
|
@class_name = class_name # For lazy loading
|
|
20
49
|
@owner = owner # The object that owns this association
|
|
@@ -22,7 +51,7 @@ module ActiveItem
|
|
|
22
51
|
@index_name = index_name
|
|
23
52
|
@limit_value = limit_value
|
|
24
53
|
@not_conditions = not_conditions.dup
|
|
25
|
-
@
|
|
54
|
+
@ilike_attributes = ilike_attributes # Attributes matched case-insensitively (substring unless ilike_exact)
|
|
26
55
|
@ilike_exact = ilike_exact # Require exact case-insensitive match (Ruby-side filter)
|
|
27
56
|
@loaded = preloaded_records ? true : false
|
|
28
57
|
@records = preloaded_records
|
|
@@ -31,6 +60,11 @@ module ActiveItem
|
|
|
31
60
|
@select_attributes = select_attributes # Projection expression attributes
|
|
32
61
|
end
|
|
33
62
|
|
|
63
|
+
# Whether any condition is matched case-insensitively.
|
|
64
|
+
def ilike
|
|
65
|
+
ilike_attributes.any?
|
|
66
|
+
end
|
|
67
|
+
|
|
34
68
|
# Whether the relation's records have been loaded from DynamoDB.
|
|
35
69
|
def loaded?
|
|
36
70
|
@loaded
|
|
@@ -75,10 +109,13 @@ module ActiveItem
|
|
|
75
109
|
new_ilike = new_conditions.delete(:ilike)
|
|
76
110
|
new_exact = new_conditions.delete(:exact)
|
|
77
111
|
|
|
112
|
+
# A condition re-specified here drops any earlier ilike on that attribute
|
|
113
|
+
kept_ilike = new_ilike == false ? [] : ilike_attributes - new_conditions.keys.map(&:to_sym)
|
|
114
|
+
|
|
78
115
|
spawn(
|
|
79
116
|
conditions: conditions.merge(new_conditions),
|
|
80
117
|
index_name: new_index || index_name,
|
|
81
|
-
|
|
118
|
+
ilike_attributes: kept_ilike | Relation.resolve_ilike_attributes(new_ilike, new_conditions),
|
|
82
119
|
ilike_exact: new_exact.nil? ? ilike_exact : new_exact
|
|
83
120
|
)
|
|
84
121
|
end
|
|
@@ -331,15 +368,32 @@ module ActiveItem
|
|
|
331
368
|
# Delegate to Enumerable#find when block is given (Rails behavior)
|
|
332
369
|
to_a.find(&)
|
|
333
370
|
elsif id
|
|
371
|
+
not_found = "Couldn't find #{resolved_model.name} with id=#{id}"
|
|
372
|
+
raise ActiveItem::RecordNotFound, not_found if conditions[:_empty]
|
|
373
|
+
|
|
374
|
+
# A loaded relation (preloaded association, has_many :through,
|
|
375
|
+
# where(id: [...])) is its own scope: look in the loaded records.
|
|
376
|
+
if @loaded
|
|
377
|
+
record = @records.find { |r| r.id.to_s == id.to_s }
|
|
378
|
+
raise ActiveItem::RecordNotFound, not_found unless record
|
|
379
|
+
|
|
380
|
+
return record
|
|
381
|
+
end
|
|
382
|
+
|
|
334
383
|
# Use direct GetItem instead of scanning — O(1) vs O(n)
|
|
335
384
|
record = resolved_model.find(id)
|
|
336
385
|
|
|
337
|
-
#
|
|
338
|
-
#
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
raise ActiveItem::RecordNotFound,
|
|
386
|
+
# Every condition and not-condition must hold, so @project.epics.find(id)
|
|
387
|
+
# only returns epics belonging to @project. Conditions we can't evaluate
|
|
388
|
+
# in Ruby (nested paths, ranges, ...) are checked by DynamoDB instead.
|
|
389
|
+
case scope_verdict(record)
|
|
390
|
+
when false
|
|
391
|
+
raise ActiveItem::RecordNotFound, not_found
|
|
392
|
+
when nil
|
|
393
|
+
pk = resolved_model.primary_key.to_sym
|
|
394
|
+
record = spawn(conditions: conditions.merge(pk => record.id), limit_value: nil, select_attributes: nil,
|
|
395
|
+
includes_associations: []).to_a.first
|
|
396
|
+
raise ActiveItem::RecordNotFound, not_found unless record
|
|
343
397
|
end
|
|
344
398
|
|
|
345
399
|
preload_associations_for_records([record]) if includes_associations.any?
|
|
@@ -477,7 +531,7 @@ module ActiveItem
|
|
|
477
531
|
parts = []
|
|
478
532
|
parts << "conditions=#{conditions.inspect}" if conditions.any?
|
|
479
533
|
parts << "not_conditions=#{not_conditions.inspect}" if not_conditions.any?
|
|
480
|
-
parts <<
|
|
534
|
+
parts << "ilike=#{ilike_attributes.inspect}" if ilike
|
|
481
535
|
parts << 'exact=true' if ilike_exact
|
|
482
536
|
"#<#{self.class.name} (not loaded) #{parts.join(' ')}>"
|
|
483
537
|
end
|
|
@@ -507,7 +561,7 @@ module ActiveItem
|
|
|
507
561
|
index_name: overrides.fetch(:index_name, index_name),
|
|
508
562
|
limit_value: overrides.fetch(:limit_value, limit_value),
|
|
509
563
|
not_conditions: overrides.fetch(:not_conditions, not_conditions),
|
|
510
|
-
|
|
564
|
+
ilike_attributes: overrides.fetch(:ilike_attributes, ilike_attributes),
|
|
511
565
|
ilike_exact: overrides.fetch(:ilike_exact, ilike_exact),
|
|
512
566
|
class_name: overrides.fetch(:class_name, class_name),
|
|
513
567
|
owner: overrides.fetch(:owner, owner),
|
|
@@ -527,13 +581,94 @@ module ActiveItem
|
|
|
527
581
|
@resolved_model ||= safe_constantize_model(@class_name)
|
|
528
582
|
end
|
|
529
583
|
|
|
584
|
+
# Whether a fetched record satisfies every condition and not-condition of
|
|
585
|
+
# this relation. Returns true/false, or nil when some condition can't be
|
|
586
|
+
# evaluated in Ruby with the same semantics DynamoDB would apply.
|
|
587
|
+
def scope_verdict(record)
|
|
588
|
+
normalize_conditions(conditions).each do |attr, expected|
|
|
589
|
+
result = condition_verdict(record, attr, expected, ilike: ilike_attribute?(attr))
|
|
590
|
+
return result unless result == true
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
normalize_conditions(not_conditions).each do |attr, expected|
|
|
594
|
+
# DynamoDB's <> / NOT IN on a missing attribute: let it decide
|
|
595
|
+
return nil if !expected.nil? && read_condition_attribute(record, attr).nil?
|
|
596
|
+
|
|
597
|
+
result = condition_verdict(record, attr, expected, ilike: false)
|
|
598
|
+
return nil if result.nil?
|
|
599
|
+
return false if result
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
true
|
|
603
|
+
end
|
|
604
|
+
|
|
605
|
+
# Ruby mirror of build_condition_expression for the value shapes whose
|
|
606
|
+
# semantics are unambiguous. Returns nil for anything else.
|
|
607
|
+
def condition_verdict(record, attr, expected, ilike:)
|
|
608
|
+
attr = attr.to_s
|
|
609
|
+
if expected.is_a?(ActiveItem::Base)
|
|
610
|
+
expected = expected.send(expected.class.primary_key)
|
|
611
|
+
attr = "#{attr}_id" unless attr.end_with?('_id')
|
|
612
|
+
end
|
|
613
|
+
return nil if attr.include?('.') || expected.is_a?(Hash) || expected.is_a?(Range)
|
|
614
|
+
return nil unless readable_condition_attribute?(record, attr)
|
|
615
|
+
|
|
616
|
+
actual = read_condition_attribute(record, attr)
|
|
617
|
+
|
|
618
|
+
if expected.nil?
|
|
619
|
+
actual.nil?
|
|
620
|
+
elsif ilike && expected.is_a?(String)
|
|
621
|
+
return false unless actual.is_a?(String)
|
|
622
|
+
|
|
623
|
+
ilike_exact ? actual.downcase == expected.downcase : actual.downcase.include?(expected.downcase)
|
|
624
|
+
elsif expected.is_a?(ActiveItem::BeginsWith)
|
|
625
|
+
actual.is_a?(String) && actual.start_with?(expected.prefix)
|
|
626
|
+
elsif expected.is_a?(Array)
|
|
627
|
+
return nil unless expected.all? { |v| scalar_condition_value?(v) }
|
|
628
|
+
|
|
629
|
+
expected.include?(actual)
|
|
630
|
+
elsif scalar_condition_value?(expected)
|
|
631
|
+
actual == expected
|
|
632
|
+
end
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
def scalar_condition_value?(value)
|
|
636
|
+
value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false
|
|
637
|
+
end
|
|
638
|
+
|
|
639
|
+
def readable_condition_attribute?(record, attr)
|
|
640
|
+
attr == 'id' || attr == record.class.primary_key.to_s || record.class.attribute_names.include?(attr)
|
|
641
|
+
end
|
|
642
|
+
|
|
643
|
+
def read_condition_attribute(record, attr)
|
|
644
|
+
attr = attr.to_s
|
|
645
|
+
return record.id if attr == 'id' || attr == record.class.primary_key.to_s
|
|
646
|
+
|
|
647
|
+
record.instance_variable_get("@#{attr}")
|
|
648
|
+
end
|
|
649
|
+
|
|
650
|
+
# Set DynamoDB's Limit only when there is no filter. Limit caps the items
|
|
651
|
+
# *read* per page, before the filter runs, so a small limit with a
|
|
652
|
+
# selective filter turns into one request per item in the table/partition.
|
|
653
|
+
# Callers keep counting matches themselves and stop once they have enough.
|
|
654
|
+
def apply_scan_limit!(params, value)
|
|
655
|
+
params[:limit] = value if value && !params[:filter_expression]
|
|
656
|
+
end
|
|
657
|
+
|
|
658
|
+
# Whether a (normalized) condition attribute is matched case-insensitively
|
|
659
|
+
def ilike_attribute?(attr)
|
|
660
|
+
ilike_attributes.any? do |name|
|
|
661
|
+
name == attr.to_sym || (resolve_association_foreign_key(name.to_s) || name.to_s) == attr.to_s
|
|
662
|
+
end
|
|
663
|
+
end
|
|
664
|
+
|
|
530
665
|
# Apply Ruby-side filter for case-insensitive matching
|
|
531
|
-
# Called after DynamoDB query returns results when ilike is
|
|
666
|
+
# Called after DynamoDB query returns results when ilike is set
|
|
532
667
|
def apply_ilike_filter(records)
|
|
533
668
|
return records unless ilike
|
|
534
669
|
|
|
535
|
-
#
|
|
536
|
-
string_conditions = conditions.select { |
|
|
670
|
+
# Only the string conditions marked ilike get case-insensitive matching
|
|
671
|
+
string_conditions = conditions.select { |k, v| v.is_a?(String) && ilike_attributes.include?(k.to_sym) }
|
|
537
672
|
return records if string_conditions.empty?
|
|
538
673
|
|
|
539
674
|
records.select do |record|
|
|
@@ -680,7 +815,7 @@ module ActiveItem
|
|
|
680
815
|
|
|
681
816
|
if remaining_conditions.any?
|
|
682
817
|
remaining_conditions.each_with_index do |(attr, val), idx|
|
|
683
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
818
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
684
819
|
filter_parts << expr
|
|
685
820
|
params[:expression_attribute_names].merge!(names)
|
|
686
821
|
params[:expression_attribute_values].merge!(values) if values.any?
|
|
@@ -705,8 +840,10 @@ module ActiveItem
|
|
|
705
840
|
|
|
706
841
|
loop do
|
|
707
842
|
params[:exclusive_start_key] = current_cursor if current_cursor
|
|
708
|
-
# Fetch more than needed to reduce round trips when filter removes items
|
|
709
|
-
|
|
843
|
+
# Fetch more than needed to reduce round trips when filter removes items.
|
|
844
|
+
# With a filter, read a full 100 per request so a tiny per_page can't
|
|
845
|
+
# turn into one request per item.
|
|
846
|
+
params[:limit] = params[:filter_expression] ? Pagination::MAX_PER_PAGE : [per_page * 2, 100].min
|
|
710
847
|
|
|
711
848
|
response = resolved_model.dynamodb.query(params)
|
|
712
849
|
items = response.items.map { |item| resolved_model.instantiate(item) }
|
|
@@ -722,15 +859,17 @@ module ActiveItem
|
|
|
722
859
|
|
|
723
860
|
# Trim to requested page size and determine next cursor
|
|
724
861
|
if collected_items.length > per_page
|
|
725
|
-
|
|
862
|
+
# Resume right after the last returned item. last_evaluated_key points past
|
|
863
|
+
# the whole fetched page and would skip the trimmed items.
|
|
864
|
+
next_cursor = build_synthetic_cursor(collected_items[per_page - 1], idx_name, index_config)
|
|
726
865
|
[collected_items.take(per_page), encode_cursor(next_cursor)]
|
|
727
866
|
else
|
|
728
867
|
[collected_items, encode_cursor(last_evaluated_key)]
|
|
729
868
|
end
|
|
730
869
|
end
|
|
731
870
|
|
|
732
|
-
# Build a synthetic ExclusiveStartKey from
|
|
733
|
-
#
|
|
871
|
+
# Build a synthetic ExclusiveStartKey from the last returned item when a
|
|
872
|
+
# page was trimmed to per_page, so the next page resumes right after it.
|
|
734
873
|
# For GSI queries, DynamoDB requires: table PK + index partition key + index sort key (if any).
|
|
735
874
|
def build_synthetic_cursor(item, _idx_name, index_config)
|
|
736
875
|
pk_dynamo = resolved_model.to_dynamo_key(resolved_model.primary_key.to_s)
|
|
@@ -748,7 +887,7 @@ module ActiveItem
|
|
|
748
887
|
filter_names = {}
|
|
749
888
|
|
|
750
889
|
normalized_conditions.each_with_index do |(attr, val), idx|
|
|
751
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
890
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
752
891
|
filter_parts << expr
|
|
753
892
|
filter_names.merge!(names)
|
|
754
893
|
filter_values.merge!(values) if values.any?
|
|
@@ -773,8 +912,10 @@ module ActiveItem
|
|
|
773
912
|
|
|
774
913
|
loop do
|
|
775
914
|
params[:exclusive_start_key] = current_cursor if current_cursor
|
|
776
|
-
# Fetch more than needed to reduce round trips when filter removes items
|
|
777
|
-
|
|
915
|
+
# Fetch more than needed to reduce round trips when filter removes items.
|
|
916
|
+
# With a filter, read a full 100 per request so a tiny per_page can't
|
|
917
|
+
# turn into one request per item.
|
|
918
|
+
params[:limit] = params[:filter_expression] ? Pagination::MAX_PER_PAGE : [per_page * 2, 100].min
|
|
778
919
|
|
|
779
920
|
response = resolved_model.dynamodb.scan(params)
|
|
780
921
|
items = response.items.map { |item| resolved_model.instantiate(item) }
|
|
@@ -851,7 +992,7 @@ module ActiveItem
|
|
|
851
992
|
build_count_scan_params(normalized_conditions)
|
|
852
993
|
end
|
|
853
994
|
params[:exclusive_start_key] = exclusive_start_key if exclusive_start_key
|
|
854
|
-
params
|
|
995
|
+
apply_scan_limit!(params, limit_value)
|
|
855
996
|
|
|
856
997
|
response = if effective_index && normalized_conditions.any?
|
|
857
998
|
resolved_model.dynamodb.query(params)
|
|
@@ -948,7 +1089,7 @@ module ActiveItem
|
|
|
948
1089
|
loop do
|
|
949
1090
|
params = build_count_scan_params(normalized_conditions)
|
|
950
1091
|
params[:exclusive_start_key] = exclusive_start_key if exclusive_start_key
|
|
951
|
-
params
|
|
1092
|
+
apply_scan_limit!(params, limit_value)
|
|
952
1093
|
|
|
953
1094
|
response = resolved_model.dynamodb.scan(params)
|
|
954
1095
|
total += response.count
|
|
@@ -1027,7 +1168,7 @@ module ActiveItem
|
|
|
1027
1168
|
|
|
1028
1169
|
if remaining_conditions.any?
|
|
1029
1170
|
remaining_conditions.each_with_index do |(attr, val), idx|
|
|
1030
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
1171
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
1031
1172
|
filter_parts << expr
|
|
1032
1173
|
params[:expression_attribute_names].merge!(names)
|
|
1033
1174
|
params[:expression_attribute_values].merge!(values) if values.any?
|
|
@@ -1044,7 +1185,7 @@ module ActiveItem
|
|
|
1044
1185
|
end
|
|
1045
1186
|
|
|
1046
1187
|
params[:filter_expression] = filter_parts.join(' AND ') if filter_parts.any?
|
|
1047
|
-
params
|
|
1188
|
+
apply_scan_limit!(params, limit_value)
|
|
1048
1189
|
params[:scan_index_forward] = (order_direction != :desc) unless order_direction.nil?
|
|
1049
1190
|
apply_projection_expression!(params)
|
|
1050
1191
|
params
|
|
@@ -1057,7 +1198,7 @@ module ActiveItem
|
|
|
1057
1198
|
filter_values = {}
|
|
1058
1199
|
|
|
1059
1200
|
normalized_conditions.each_with_index do |(attr, val), idx|
|
|
1060
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
1201
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
1061
1202
|
filter_parts << expr
|
|
1062
1203
|
filter_names.merge!(names)
|
|
1063
1204
|
filter_values.merge!(values) if values.any?
|
|
@@ -1078,7 +1219,7 @@ module ActiveItem
|
|
|
1078
1219
|
params[:expression_attribute_names] = filter_names if filter_names.any?
|
|
1079
1220
|
params[:expression_attribute_values] = filter_values if filter_values.any?
|
|
1080
1221
|
params[:projection_expression] = proj_expr if proj_expr
|
|
1081
|
-
params
|
|
1222
|
+
apply_scan_limit!(params, limit_value)
|
|
1082
1223
|
params
|
|
1083
1224
|
end
|
|
1084
1225
|
|
|
@@ -1188,7 +1329,7 @@ module ActiveItem
|
|
|
1188
1329
|
|
|
1189
1330
|
if remaining_conditions.any?
|
|
1190
1331
|
remaining_conditions.each_with_index do |(attr, val), idx|
|
|
1191
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
1332
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
1192
1333
|
filter_parts << expr
|
|
1193
1334
|
params[:expression_attribute_names].merge!(names)
|
|
1194
1335
|
params[:expression_attribute_values].merge!(values) if values.any?
|
|
@@ -1206,7 +1347,7 @@ module ActiveItem
|
|
|
1206
1347
|
end
|
|
1207
1348
|
|
|
1208
1349
|
params[:filter_expression] = filter_parts.join(' AND ') if filter_parts.any?
|
|
1209
|
-
params
|
|
1350
|
+
apply_scan_limit!(params, limit_value)
|
|
1210
1351
|
params[:scan_index_forward] = (order_direction != :desc) unless order_direction.nil?
|
|
1211
1352
|
|
|
1212
1353
|
# Add projection expression if select() was used
|
|
@@ -1225,7 +1366,7 @@ module ActiveItem
|
|
|
1225
1366
|
break if limit_value && all_items.length >= limit_value
|
|
1226
1367
|
end
|
|
1227
1368
|
|
|
1228
|
-
all_items
|
|
1369
|
+
limit_value ? all_items.first(limit_value) : all_items
|
|
1229
1370
|
end
|
|
1230
1371
|
|
|
1231
1372
|
# Fan-out parallel queries when partition key is an Array.
|
|
@@ -1254,7 +1395,7 @@ module ActiveItem
|
|
|
1254
1395
|
|
|
1255
1396
|
apply_sort_key_to_params!(params, sort_key, remaining_conditions)
|
|
1256
1397
|
params[:filter_expression] = filter_parts.join(' AND ') if filter_parts.any?
|
|
1257
|
-
params
|
|
1398
|
+
apply_scan_limit!(params, limit_value)
|
|
1258
1399
|
params[:scan_index_forward] = (order_direction != :desc) unless order_direction.nil?
|
|
1259
1400
|
apply_projection_expression!(params)
|
|
1260
1401
|
|
|
@@ -1375,7 +1516,7 @@ module ActiveItem
|
|
|
1375
1516
|
end
|
|
1376
1517
|
|
|
1377
1518
|
non_sort_conditions.each_with_index do |(attr, val), idx|
|
|
1378
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
1519
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
1379
1520
|
filter_parts << expr
|
|
1380
1521
|
filter_names.merge!(names)
|
|
1381
1522
|
filter_values.merge!(values) if values.any?
|
|
@@ -1503,7 +1644,7 @@ module ActiveItem
|
|
|
1503
1644
|
filter_names = {}
|
|
1504
1645
|
|
|
1505
1646
|
normalized_conditions.each_with_index do |(attr, val), idx|
|
|
1506
|
-
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike:
|
|
1647
|
+
expr, names, values = resolved_model.send(:build_condition_expression, attr, val, idx, ilike: ilike_attribute?(attr))
|
|
1507
1648
|
filter_parts << expr
|
|
1508
1649
|
filter_names.merge!(names)
|
|
1509
1650
|
filter_values.merge!(values) if values.any?
|
|
@@ -46,7 +46,9 @@ module ActiveItem
|
|
|
46
46
|
!current.nil?
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
# @param condition [String, nil] ConditionExpression for the put
|
|
50
|
+
# @param expression_attribute_names [Hash, nil] Name placeholders used by +condition+
|
|
51
|
+
def put(record, condition: nil, expression_attribute_names: nil)
|
|
50
52
|
record.instance_variable_set(:@id, SecureRandom.uuid) unless record.id
|
|
51
53
|
pk = record.class.primary_key
|
|
52
54
|
record.instance_variable_set(:"@#{pk}", record.id) if pk != 'id'
|
|
@@ -62,6 +64,7 @@ module ActiveItem
|
|
|
62
64
|
|
|
63
65
|
op = { put: { table_name: record.class.table_name, item: item } }
|
|
64
66
|
op[:put][:condition_expression] = condition if condition
|
|
67
|
+
op[:put][:expression_attribute_names] = expression_attribute_names if expression_attribute_names
|
|
65
68
|
|
|
66
69
|
@operations << { op: op, record: record, type: :put }
|
|
67
70
|
end
|
data/lib/active_item/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activeitem
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.26
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Davis
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2026-11-
|
|
12
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activemodel
|