Class: Lyra::Projections::EventStoreReader
- Inherits:
-
Object
- Object
- Lyra::Projections::EventStoreReader
- Defined in:
- lib/lyra/projections/event_store_reader.rb
Overview
Reads model state from event store when projections are disabled.
Uses CachedProjection (backed by Solid Cache or Rails.cache) for fast reads while keeping events as the source of truth.
Usage:
EventStoreReader.find(User, 123)
EventStoreReader.find_by(User, email: "[email protected]")
EventStoreReader.where(User, status: "active")
Class Method Summary collapse
-
.all(model_class) ⇒ CachedRelation
Load all records of a given type (cached) Warning: Can be expensive for large datasets.
-
.count(model_class, conditions = {}) ⇒ Integer
Count records (cached).
-
.exists?(model_class, id) ⇒ Boolean
Check if a record exists (cached).
-
.find(model_class, id) ⇒ ActiveRecord::Base?
Find a record by ID (cached).
-
.find_by(model_class, attributes) ⇒ ActiveRecord::Base?
Find a record by attributes (cached).
-
.invalidate(model_class, id) ⇒ Object
Invalidate cache for a record (call after event stored).
-
.relation(model_class) ⇒ CachedRelation
Get a CachedRelation for the model (supports method chaining).
-
.warm(model_class, id) ⇒ Object
Warm cache for a record (call after event stored).
-
.where(model_class, conditions) ⇒ CachedRelation
Query records with conditions (cached).
Class Method Details
.all(model_class) ⇒ CachedRelation
Load all records of a given type (cached) Warning: Can be expensive for large datasets
65 66 67 |
# File 'lib/lyra/projections/event_store_reader.rb', line 65 def all(model_class) relation(model_class) end |
.count(model_class, conditions = {}) ⇒ Integer
Count records (cached)
83 84 85 |
# File 'lib/lyra/projections/event_store_reader.rb', line 83 def count(model_class, conditions = {}) CachedProjection.count(model_class, conditions) end |
.exists?(model_class, id) ⇒ Boolean
Check if a record exists (cached)
46 47 48 |
# File 'lib/lyra/projections/event_store_reader.rb', line 46 def exists?(model_class, id) CachedProjection.exists?(model_class, id) end |
.find(model_class, id) ⇒ ActiveRecord::Base?
Find a record by ID (cached)
22 23 24 25 26 27 |
# File 'lib/lyra/projections/event_store_reader.rb', line 22 def find(model_class, id) attributes = CachedProjection.find(model_class, id) return nil unless attributes build_instance(model_class, attributes) end |
.find_by(model_class, attributes) ⇒ ActiveRecord::Base?
Find a record by attributes (cached)
34 35 36 37 38 39 |
# File 'lib/lyra/projections/event_store_reader.rb', line 34 def find_by(model_class, attributes) result = CachedProjection.find_by(model_class, attributes) return nil unless result build_instance(model_class, result) end |
.invalidate(model_class, id) ⇒ Object
Invalidate cache for a record (call after event stored)
91 92 93 |
# File 'lib/lyra/projections/event_store_reader.rb', line 91 def invalidate(model_class, id) CachedProjection.invalidate(model_class, id) end |
.relation(model_class) ⇒ CachedRelation
Get a CachedRelation for the model (supports method chaining)
54 55 56 57 58 |
# File 'lib/lyra/projections/event_store_reader.rb', line 54 def relation(model_class) results = CachedProjection.all(model_class) records = results.map { |attrs| build_instance(model_class, attrs) }.compact CachedRelation.new(model_class, records) end |
.warm(model_class, id) ⇒ Object
Warm cache for a record (call after event stored)
99 100 101 |
# File 'lib/lyra/projections/event_store_reader.rb', line 99 def warm(model_class, id) CachedProjection.warm(model_class, id) end |
.where(model_class, conditions) ⇒ CachedRelation
Query records with conditions (cached)
74 75 76 |
# File 'lib/lyra/projections/event_store_reader.rb', line 74 def where(model_class, conditions) relation(model_class).where(conditions) end |