Class: EntitySnapshot::Postgres

Inherits:
Object
  • Object
show all
Includes:
EntityCache::Store::External, Get, StreamName, Log::Dependency
Defined in:
lib/entity_snapshot/postgres/get.rb,
lib/entity_snapshot/postgres/log.rb,
lib/entity_snapshot/postgres/postgres.rb,
lib/entity_snapshot/postgres/read_only.rb,
lib/entity_snapshot/postgres/controls/id.rb,
lib/entity_snapshot/postgres/stream_name.rb,
lib/entity_snapshot/postgres/controls/time.rb,
lib/entity_snapshot/postgres/controls/batch.rb,
lib/entity_snapshot/postgres/controls/write.rb,
lib/entity_snapshot/postgres/controls/entity.rb,
lib/entity_snapshot/postgres/controls/message.rb,
lib/entity_snapshot/postgres/controls/version.rb,
lib/entity_snapshot/postgres/controls/snapshot.rb,
lib/entity_snapshot/postgres/controls/stream_name.rb,
lib/entity_snapshot/postgres/controls/entity_store.rb,
lib/entity_snapshot/postgres/controls/random_value.rb,
lib/entity_snapshot/postgres/controls/snapshot/put.rb,
lib/entity_snapshot/postgres/controls/snapshot/read_only.rb

Defined Under Namespace

Modules: Controls, Get, StreamName Classes: Log, ReadOnly

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StreamName

included, #snapshot_stream_name

Methods included from Get

#get, prepended

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



13
14
15
# File 'lib/entity_snapshot/postgres/postgres.rb', line 13

def session
  @session
end

Instance Method Details

#categoryObject



17
18
19
20
21
# File 'lib/entity_snapshot/postgres/postgres.rb', line 17

def category
  *, entity_class_name = entity_class.name.split('::')

  Casing::Camel.(entity_class_name)
end

#configure(session: nil) ⇒ Object



23
24
25
26
27
# File 'lib/entity_snapshot/postgres/postgres.rb', line 23

def configure(session: nil)
  MessageStore::Postgres::Session.configure(self, session: session)
  MessageStore::Postgres::Put.configure(self, session: self.session, attr_name: :write)
  MessageStore::Postgres::Get::Last.configure(self, session: self.session, attr_name: :read)
end

#put(id, entity, version, time) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/entity_snapshot/postgres/postgres.rb', line 29

def put(id, entity, version, time)
  unless entity.is_a? subject
    error_msg = "Persistent storage for #{subject} cannot store #{entity}"
    logger.error() { error_msg }
    raise Error, error_msg
  end

  stream_name = snapshot_stream_name(id)

  logger.trace(tags: [:snapshot, :cache, :put]) { "Writing snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" }

  entity_data = Transform::Write.raw_data(entity)

  event_data = MessageStore::MessageData::Write.new

  iso8601_time = Clock::UTC.iso8601(time)

  data = {
    entity_data: entity_data,
    entity_version: version,
    time: iso8601_time
  }

  event_data.type = 'Recorded'
  event_data.data = data

  position = write.(event_data, stream_name)

  logger.debug(tags: [:snapshot, :cache, :put]) { "Wrote snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" }

  position
end