Class: Lyra::Projections::EventStoreReader

Inherits:
Object
  • Object
show all
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

Class Method Details

.all(model_class) ⇒ CachedRelation

Load all records of a given type (cached) Warning: Can be expensive for large datasets

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

Returns:

  • (CachedRelation) —

    A relation containing all reconstructed records



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • conditions (Hash) (defaults to: {}) —

    Optional conditions

Returns:

  • (Integer) —

    Count of matching records



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • id (Integer, String) —

    The record ID

Returns:

  • (Boolean) —

    True if record exists and is not destroyed



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • id (Integer, String) —

    The record ID

Returns:

  • (ActiveRecord::Base, nil) —

    The reconstructed record or nil



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • attributes (Hash) —

    The attributes to match

Returns:

  • (ActiveRecord::Base, nil) —

    The first matching record or nil



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • id (Integer, String) —

    The record ID



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

Returns:



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • id (Integer, String) —

    The record ID



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)

Parameters:

  • model_class (Class) —

    The ActiveRecord model class

  • conditions (Hash) —

    Query conditions

Returns:



74
75
76
# File 'lib/lyra/projections/event_store_reader.rb', line 74

def where(model_class, conditions)
  relation(model_class).where(conditions)
end