Class: Mammoth::TransactionEnvelopeSerializer
- Inherits:
-
Object
- Object
- Mammoth::TransactionEnvelopeSerializer
- Defined in:
- lib/mammoth/transaction_envelope_serializer.rb
Overview
Serializes CDC transaction envelopes into webhook payloads.
Mammoth uses transaction envelopes as the safest delivery and checkpointing boundary for concurrent delivery. A transaction payload preserves the commit position and groups the row-level changes that belong to the same database transaction.
Constant Summary collapse
- PAYLOAD_TYPE =
Default payload type for transaction webhook delivery.
"transaction.committed"
Class Method Summary collapse
-
.call(envelope) ⇒ Hash
Serialize a CDC::Core::TransactionEnvelope-like object into a Hash.
Instance Method Summary collapse
-
#call ⇒ Hash
Return the webhook payload.
-
#initialize(envelope) ⇒ TransactionEnvelopeSerializer
constructor
A new instance of TransactionEnvelopeSerializer.
-
#to_json(*_args) ⇒ String
Return JSON representation of the transaction payload.
Constructor Details
#initialize(envelope) ⇒ TransactionEnvelopeSerializer
Returns a new instance of TransactionEnvelopeSerializer.
27 28 29 |
# File 'lib/mammoth/transaction_envelope_serializer.rb', line 27 def initialize(envelope) @envelope = envelope end |
Class Method Details
.call(envelope) ⇒ Hash
Serialize a CDC::Core::TransactionEnvelope-like object into a Hash.
22 23 24 |
# File 'lib/mammoth/transaction_envelope_serializer.rb', line 22 def self.call(envelope) new(envelope).call end |
Instance Method Details
#call ⇒ Hash
Return the webhook payload.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mammoth/transaction_envelope_serializer.rb', line 34 def call event_payloads = envelope.events.map { |event| EventSerializer.call(event) } { "event_id" => envelope_value("event_id") || SecureRandom.uuid, "type" => PAYLOAD_TYPE, "source" => first_event_value(event_payloads, "source") || EventSerializer::DEFAULT_SOURCE, "transaction_id" => envelope.transaction_id, "source_position" => source_position(event_payloads), "commit_lsn" => source_position(event_payloads), "committed_at" => committed_at, "event_count" => event_payloads.length, "events" => event_payloads, "metadata" => envelope_value("metadata") || {} } end |
#to_json(*_args) ⇒ String
Return JSON representation of the transaction payload.
53 54 55 |
# File 'lib/mammoth/transaction_envelope_serializer.rb', line 53 def to_json(*_args) JSON.generate(call) end |