class Synapse::EventStore::Mongo::StorageStrategy

Represents a mechanism used to structure how events are stored in the database @abstract

Constants

ASCENDING

Aliases of the Mongo constants for ascending and descending

DESCENDING

Public Class Methods

new(template, serializer, upcaster_chain) click to toggle source

@param [MongoTemplate] template @param [Serializer] serializer @param [UpcasterChain] upcaster_chain @return [undefined]

# File lib/synapse/event_store/mongo/storage_strategy.rb, line 11
def initialize(template, serializer, upcaster_chain)
  @template = template
  @serializer = Serialization::MessageSerializer.new serializer
  @upcaster_chain = upcaster_chain
end

Public Instance Methods

create_documents(type_identifier, events) click to toggle source

Creates documents that will represent the events being committed to the event store

@abstract @param [String] type_identifier Type identifier for the aggregate @param [Array] events Domain events to be committed @return [Array]

# File lib/synapse/event_store/mongo/storage_strategy.rb, line 23
def create_documents(type_identifier, events); end
ensure_indexes() click to toggle source

Ensures that the correct indexes are in place @return [undefined]

# File lib/synapse/event_store/mongo/storage_strategy.rb, line 88
def ensure_indexes
  options = {
    name: 'unique_aggregate_index',
    unique: true
  }

  spec = {
    aggregate_id: ASCENDING,
    aggregate_type: ASCENDING,
    sequence_number: ASCENDING
  }

  @template.event_collection.ensure_index spec, options

  spec = {
    aggregate_id: ASCENDING,
    aggregate_type: ASCENDING,
    sequence_number: DESCENDING
  }

  @template.snapshot_collection.ensure_index spec, options
end
extract_events(document, aggregate_id) click to toggle source

Extracts individual event messages from the given document

The given aggregate identifier is passed so that event messages can have the actual identifier object instead of the serialized aggregate identifier.

@abstract @param [Hash] document @param [Object] aggregate_id @return [Array]

# File lib/synapse/event_store/mongo/storage_strategy.rb, line 34
def extract_events(document, aggregate_id); end
fetch_events(type_identifier, aggregate_id, first_sequence_number) click to toggle source

Provides a cursor for accessing all events for an aggregate with the given identifier and type identifier, with a sequence number equal to or greater than the given first sequence number

The returned documents should be ordered chronologically, typically by using the sequence number.

@param [String] type_identifier @param [Object] aggregate_id @param [Integer] first_sequence_number @return [Mongo::Cursor]

# File lib/synapse/event_store/mongo/storage_strategy.rb, line 51
def fetch_events(type_identifier, aggregate_id, first_sequence_number)
  filter = {
    aggregate_id: aggregate_id,
    aggregate_type: type_identifier,
    sequence_number: {
      '$gte' => first_sequence_number
    }
  }

  sort = {
    sequence_number: ASCENDING
  }

  @template.event_collection.find(filter).sort(sort)
end
fetch_last_snapshot(type_identifier, aggregate_id) click to toggle source

Finds the document containing the most recent snapshot event for an aggregate with the given identifier and type identifier

@param [String] type_identifier @param [Object] aggregate_id @return [Mongo::Cursor]

# File lib/synapse/event_store/mongo/storage_strategy.rb, line 73
def fetch_last_snapshot(type_identifier, aggregate_id)
  filter = {
    aggregate_id: aggregate_id,
    aggregate_type: type_identifier
  }

  sort = {
    sequence_number: DESCENDING
  }

  @template.snapshot_collection.find(filter).sort(sort).limit(1)
end