Class: Synapse::EventStore::Mongo::DocumentPerCommitStrategy::CommitDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/event_store/mongo/per_commit_strategy.rb

Overview

Mongo document that represents a commit containing one or more events

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aggregate_idObject (readonly)

Returns:

  • (Object)


31
32
33
# File 'lib/synapse/event_store/mongo/per_commit_strategy.rb', line 31

def aggregate_id
  @aggregate_id
end

Instance Method Details

#from_events(type_identifier, events, serializer) ⇒ CommitDocument

Parameters:

  • type_identifier (String)
  • events (Array)
  • serializer (Serializer)

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/synapse/event_store/mongo/per_commit_strategy.rb', line 37

def from_events(type_identifier, events, serializer)
  first_event = events.first
  last_event = events.last

  @aggregate_type = type_identifier
  @aggregate_id = first_event.aggregate_id.to_s
  @first_sequence_number = first_event.sequence_number
  @last_sequence_number = last_event.sequence_number
  @first_timestamp = first_event.timestamp
  @last_timestamp = last_event.timestamp

  @events = Array.new
  events.each do |event|
    event_document = EventDocument.new
    event_document.from_event event, serializer

    @events.push event_document
  end

  self
end

#from_hash(hash) ⇒ CommitDocument

Parameters:

  • hash (Hash)

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/synapse/event_store/mongo/per_commit_strategy.rb', line 61

def from_hash(hash)
  hash.symbolize_keys!

  @aggregate_id = hash.fetch :aggregate_id
  @aggregate_type = hash.fetch :aggregate_type
  @first_sequence_number = hash.fetch :first_sequence_number
  @last_sequence_number = hash.fetch :last_sequence_number
  @first_timestamp = hash.fetch :first_timestamp
  @last_timestamp = hash.fetch :last_timestamp

  @events = Array.new

  event_hashes = hash.fetch :events
  event_hashes.each do |event_hash|
    event_document = EventDocument.new
    event_document.from_hash event_hash

    @events.push event_document
  end

  self
end

#to_events(aggregate_id, serializer, upcaster_chain) ⇒ Array

Parameters:

  • aggregate_id (Object)

    The actual aggregate identifier used to query the evnet store

  • serializer (Serializer)
  • upcaster_chain (UpcasterChain)

Returns:

  • (Array)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/synapse/event_store/mongo/per_commit_strategy.rb', line 108

def to_events(aggregate_id, serializer, upcaster_chain)
  events = Array.new

  @events.each do |event_document|
    event_data = DocumentDomainEventData.new aggregate_id, event_document
    context = Upcasting::SerializedDomainEventUpcastingContext.new event_data, aggregate_id, serializer

    upcast_objects = upcaster_chain.upcast event_document.payload, context
    upcast_objects.each do |upcast_object|
      upcast_data = Upcasting::UpcastSerializedDomainEventData.new event_data, aggregate_id, upcast_object

      builder = Serialization::SerializedDomainEventMessageBuilder.new

      # Prevent duplicate serialization of metadata if it was accessed during upcasting
       = context.
      if .deserialized?
        builder. = Serialization::DeserializedObject.new .deserialized
      end

      builder.from_data upcast_data, serializer

      events.push builder.build
    end
  end

  events
end

#to_hashHash

Returns:

  • (Hash)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/synapse/event_store/mongo/per_commit_strategy.rb', line 85

def to_hash
  events = Array.new
  @events.each do |event|
    events.push event.to_hash
  end

  { aggregate_id: @aggregate_id,
    aggregate_type: @aggregate_type,
    # Allows us to use the same query to filter events as DocumentPerEvent
    sequence_number: @first_sequence_number,
    first_sequence_number: @first_sequence_number,
    last_sequence_number: @last_sequence_number,
    # Allows us to use the same query to filter events as DocumentPerEvent
    timestamp: @first_timestamp,
    first_timestamp: @first_timestamp,
    last_timestamp: @last_timestamp,
    events: events }
end