activeitem 0.0.24 → 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 +26 -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 +187 -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,31 @@
|
|
|
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
|
+
|
|
23
|
+
## 0.0.25
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- **`Relation#first` could return an unrelated record on a preloaded relation.** `where(id: [...])` takes the `batch_find` optimization and returns a Relation that is already loaded with the requested records but whose `conditions` hash is empty. `#first` routed through `limit(1)` → `spawn`, producing a fresh, condition-less relation that scanned the table and returned an arbitrary record — often one belonging to a different owner. `#each`, `#length`, `#to_a`, and `#last` were unaffected, so code that iterated to authorize and then read `relation.first.some_id` would silently pick up a stranger's value. `#first` now reads directly from the loaded records when the relation is already loaded.
|
|
28
|
+
|
|
3
29
|
## 0.0.24
|
|
4
30
|
|
|
5
31
|
### Added
|
|
@@ -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
|
|
@@ -192,7 +229,15 @@ module ActiveItem
|
|
|
192
229
|
end
|
|
193
230
|
|
|
194
231
|
# Get first record
|
|
232
|
+
#
|
|
233
|
+
# When the relation is already loaded (e.g. built from batch_find via
|
|
234
|
+
# where(id: [...]), which stores preloaded_records and leaves conditions
|
|
235
|
+
# empty), read directly from the loaded records. Routing through
|
|
236
|
+
# limit(1).to_a would spawn a fresh, condition-less relation that ignores
|
|
237
|
+
# the preload and scans the table, returning an arbitrary unrelated record.
|
|
195
238
|
def first
|
|
239
|
+
return @records.first if @loaded
|
|
240
|
+
|
|
196
241
|
limit(1).to_a.first
|
|
197
242
|
end
|
|
198
243
|
|
|
@@ -323,15 +368,32 @@ module ActiveItem
|
|
|
323
368
|
# Delegate to Enumerable#find when block is given (Rails behavior)
|
|
324
369
|
to_a.find(&)
|
|
325
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
|
+
|
|
326
383
|
# Use direct GetItem instead of scanning — O(1) vs O(n)
|
|
327
384
|
record = resolved_model.find(id)
|
|
328
385
|
|
|
329
|
-
#
|
|
330
|
-
#
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
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
|
|
335
397
|
end
|
|
336
398
|
|
|
337
399
|
preload_associations_for_records([record]) if includes_associations.any?
|
|
@@ -469,7 +531,7 @@ module ActiveItem
|
|
|
469
531
|
parts = []
|
|
470
532
|
parts << "conditions=#{conditions.inspect}" if conditions.any?
|
|
471
533
|
parts << "not_conditions=#{not_conditions.inspect}" if not_conditions.any?
|
|
472
|
-
parts <<
|
|
534
|
+
parts << "ilike=#{ilike_attributes.inspect}" if ilike
|
|
473
535
|
parts << 'exact=true' if ilike_exact
|
|
474
536
|
"#<#{self.class.name} (not loaded) #{parts.join(' ')}>"
|
|
475
537
|
end
|
|
@@ -499,7 +561,7 @@ module ActiveItem
|
|
|
499
561
|
index_name: overrides.fetch(:index_name, index_name),
|
|
500
562
|
limit_value: overrides.fetch(:limit_value, limit_value),
|
|
501
563
|
not_conditions: overrides.fetch(:not_conditions, not_conditions),
|
|
502
|
-
|
|
564
|
+
ilike_attributes: overrides.fetch(:ilike_attributes, ilike_attributes),
|
|
503
565
|
ilike_exact: overrides.fetch(:ilike_exact, ilike_exact),
|
|
504
566
|
class_name: overrides.fetch(:class_name, class_name),
|
|
505
567
|
owner: overrides.fetch(:owner, owner),
|
|
@@ -519,13 +581,94 @@ module ActiveItem
|
|
|
519
581
|
@resolved_model ||= safe_constantize_model(@class_name)
|
|
520
582
|
end
|
|
521
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
|
+
|
|
522
665
|
# Apply Ruby-side filter for case-insensitive matching
|
|
523
|
-
# Called after DynamoDB query returns results when ilike is
|
|
666
|
+
# Called after DynamoDB query returns results when ilike is set
|
|
524
667
|
def apply_ilike_filter(records)
|
|
525
668
|
return records unless ilike
|
|
526
669
|
|
|
527
|
-
#
|
|
528
|
-
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) }
|
|
529
672
|
return records if string_conditions.empty?
|
|
530
673
|
|
|
531
674
|
records.select do |record|
|
|
@@ -672,7 +815,7 @@ module ActiveItem
|
|
|
672
815
|
|
|
673
816
|
if remaining_conditions.any?
|
|
674
817
|
remaining_conditions.each_with_index do |(attr, val), idx|
|
|
675
|
-
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))
|
|
676
819
|
filter_parts << expr
|
|
677
820
|
params[:expression_attribute_names].merge!(names)
|
|
678
821
|
params[:expression_attribute_values].merge!(values) if values.any?
|
|
@@ -697,8 +840,10 @@ module ActiveItem
|
|
|
697
840
|
|
|
698
841
|
loop do
|
|
699
842
|
params[:exclusive_start_key] = current_cursor if current_cursor
|
|
700
|
-
# Fetch more than needed to reduce round trips when filter removes items
|
|
701
|
-
|
|
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
|
|
702
847
|
|
|
703
848
|
response = resolved_model.dynamodb.query(params)
|
|
704
849
|
items = response.items.map { |item| resolved_model.instantiate(item) }
|
|
@@ -714,15 +859,17 @@ module ActiveItem
|
|
|
714
859
|
|
|
715
860
|
# Trim to requested page size and determine next cursor
|
|
716
861
|
if collected_items.length > per_page
|
|
717
|
-
|
|
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)
|
|
718
865
|
[collected_items.take(per_page), encode_cursor(next_cursor)]
|
|
719
866
|
else
|
|
720
867
|
[collected_items, encode_cursor(last_evaluated_key)]
|
|
721
868
|
end
|
|
722
869
|
end
|
|
723
870
|
|
|
724
|
-
# Build a synthetic ExclusiveStartKey from
|
|
725
|
-
#
|
|
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.
|
|
726
873
|
# For GSI queries, DynamoDB requires: table PK + index partition key + index sort key (if any).
|
|
727
874
|
def build_synthetic_cursor(item, _idx_name, index_config)
|
|
728
875
|
pk_dynamo = resolved_model.to_dynamo_key(resolved_model.primary_key.to_s)
|
|
@@ -740,7 +887,7 @@ module ActiveItem
|
|
|
740
887
|
filter_names = {}
|
|
741
888
|
|
|
742
889
|
normalized_conditions.each_with_index do |(attr, val), idx|
|
|
743
|
-
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))
|
|
744
891
|
filter_parts << expr
|
|
745
892
|
filter_names.merge!(names)
|
|
746
893
|
filter_values.merge!(values) if values.any?
|
|
@@ -765,8 +912,10 @@ module ActiveItem
|
|
|
765
912
|
|
|
766
913
|
loop do
|
|
767
914
|
params[:exclusive_start_key] = current_cursor if current_cursor
|
|
768
|
-
# Fetch more than needed to reduce round trips when filter removes items
|
|
769
|
-
|
|
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
|
|
770
919
|
|
|
771
920
|
response = resolved_model.dynamodb.scan(params)
|
|
772
921
|
items = response.items.map { |item| resolved_model.instantiate(item) }
|
|
@@ -843,7 +992,7 @@ module ActiveItem
|
|
|
843
992
|
build_count_scan_params(normalized_conditions)
|
|
844
993
|
end
|
|
845
994
|
params[:exclusive_start_key] = exclusive_start_key if exclusive_start_key
|
|
846
|
-
params
|
|
995
|
+
apply_scan_limit!(params, limit_value)
|
|
847
996
|
|
|
848
997
|
response = if effective_index && normalized_conditions.any?
|
|
849
998
|
resolved_model.dynamodb.query(params)
|
|
@@ -940,7 +1089,7 @@ module ActiveItem
|
|
|
940
1089
|
loop do
|
|
941
1090
|
params = build_count_scan_params(normalized_conditions)
|
|
942
1091
|
params[:exclusive_start_key] = exclusive_start_key if exclusive_start_key
|
|
943
|
-
params
|
|
1092
|
+
apply_scan_limit!(params, limit_value)
|
|
944
1093
|
|
|
945
1094
|
response = resolved_model.dynamodb.scan(params)
|
|
946
1095
|
total += response.count
|
|
@@ -1019,7 +1168,7 @@ module ActiveItem
|
|
|
1019
1168
|
|
|
1020
1169
|
if remaining_conditions.any?
|
|
1021
1170
|
remaining_conditions.each_with_index do |(attr, val), idx|
|
|
1022
|
-
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))
|
|
1023
1172
|
filter_parts << expr
|
|
1024
1173
|
params[:expression_attribute_names].merge!(names)
|
|
1025
1174
|
params[:expression_attribute_values].merge!(values) if values.any?
|
|
@@ -1036,7 +1185,7 @@ module ActiveItem
|
|
|
1036
1185
|
end
|
|
1037
1186
|
|
|
1038
1187
|
params[:filter_expression] = filter_parts.join(' AND ') if filter_parts.any?
|
|
1039
|
-
params
|
|
1188
|
+
apply_scan_limit!(params, limit_value)
|
|
1040
1189
|
params[:scan_index_forward] = (order_direction != :desc) unless order_direction.nil?
|
|
1041
1190
|
apply_projection_expression!(params)
|
|
1042
1191
|
params
|
|
@@ -1049,7 +1198,7 @@ module ActiveItem
|
|
|
1049
1198
|
filter_values = {}
|
|
1050
1199
|
|
|
1051
1200
|
normalized_conditions.each_with_index do |(attr, val), idx|
|
|
1052
|
-
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))
|
|
1053
1202
|
filter_parts << expr
|
|
1054
1203
|
filter_names.merge!(names)
|
|
1055
1204
|
filter_values.merge!(values) if values.any?
|
|
@@ -1070,7 +1219,7 @@ module ActiveItem
|
|
|
1070
1219
|
params[:expression_attribute_names] = filter_names if filter_names.any?
|
|
1071
1220
|
params[:expression_attribute_values] = filter_values if filter_values.any?
|
|
1072
1221
|
params[:projection_expression] = proj_expr if proj_expr
|
|
1073
|
-
params
|
|
1222
|
+
apply_scan_limit!(params, limit_value)
|
|
1074
1223
|
params
|
|
1075
1224
|
end
|
|
1076
1225
|
|
|
@@ -1180,7 +1329,7 @@ module ActiveItem
|
|
|
1180
1329
|
|
|
1181
1330
|
if remaining_conditions.any?
|
|
1182
1331
|
remaining_conditions.each_with_index do |(attr, val), idx|
|
|
1183
|
-
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))
|
|
1184
1333
|
filter_parts << expr
|
|
1185
1334
|
params[:expression_attribute_names].merge!(names)
|
|
1186
1335
|
params[:expression_attribute_values].merge!(values) if values.any?
|
|
@@ -1198,7 +1347,7 @@ module ActiveItem
|
|
|
1198
1347
|
end
|
|
1199
1348
|
|
|
1200
1349
|
params[:filter_expression] = filter_parts.join(' AND ') if filter_parts.any?
|
|
1201
|
-
params
|
|
1350
|
+
apply_scan_limit!(params, limit_value)
|
|
1202
1351
|
params[:scan_index_forward] = (order_direction != :desc) unless order_direction.nil?
|
|
1203
1352
|
|
|
1204
1353
|
# Add projection expression if select() was used
|
|
@@ -1217,7 +1366,7 @@ module ActiveItem
|
|
|
1217
1366
|
break if limit_value && all_items.length >= limit_value
|
|
1218
1367
|
end
|
|
1219
1368
|
|
|
1220
|
-
all_items
|
|
1369
|
+
limit_value ? all_items.first(limit_value) : all_items
|
|
1221
1370
|
end
|
|
1222
1371
|
|
|
1223
1372
|
# Fan-out parallel queries when partition key is an Array.
|
|
@@ -1246,7 +1395,7 @@ module ActiveItem
|
|
|
1246
1395
|
|
|
1247
1396
|
apply_sort_key_to_params!(params, sort_key, remaining_conditions)
|
|
1248
1397
|
params[:filter_expression] = filter_parts.join(' AND ') if filter_parts.any?
|
|
1249
|
-
params
|
|
1398
|
+
apply_scan_limit!(params, limit_value)
|
|
1250
1399
|
params[:scan_index_forward] = (order_direction != :desc) unless order_direction.nil?
|
|
1251
1400
|
apply_projection_expression!(params)
|
|
1252
1401
|
|
|
@@ -1367,7 +1516,7 @@ module ActiveItem
|
|
|
1367
1516
|
end
|
|
1368
1517
|
|
|
1369
1518
|
non_sort_conditions.each_with_index do |(attr, val), idx|
|
|
1370
|
-
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))
|
|
1371
1520
|
filter_parts << expr
|
|
1372
1521
|
filter_names.merge!(names)
|
|
1373
1522
|
filter_values.merge!(values) if values.any?
|
|
@@ -1495,7 +1644,7 @@ module ActiveItem
|
|
|
1495
1644
|
filter_names = {}
|
|
1496
1645
|
|
|
1497
1646
|
normalized_conditions.each_with_index do |(attr, val), idx|
|
|
1498
|
-
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))
|
|
1499
1648
|
filter_parts << expr
|
|
1500
1649
|
filter_names.merge!(names)
|
|
1501
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-
|
|
12
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activemodel
|