Class: PgEventstore::Client
- Inherits:
-
Object
- Object
- PgEventstore::Client
- Defined in:
- lib/pg_eventstore/client.rb
Instance Attribute Summary collapse
- #config ⇒ PgEventstore::Config writeonly
Instance Method Summary collapse
-
#append_to_stream(stream, events_or_event, options: {}, middlewares: nil) ⇒ PgEventstore::Event+
Append the event or multiple events to the stream.
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
-
#link_to(stream, events_or_event, options: {}, middlewares: []) ⇒ PgEventstore::Event+
Links event from one stream into another stream.
-
#multiple(&blk) ⇒ Object
Allows you to make several different commands atomic by wrapping then into a block.
-
#read(stream, options: {}, middlewares: nil) ⇒ Array<PgEventstore::Event>
Read events from the specific stream or from “all” stream.
-
#read_grouped(stream, options: {}, middlewares: nil) ⇒ Array<PgEventstore::Event>
Takes a stream, determines a list of even types in it and returns most recent(or very first - depending on :direction option) events, one of each type.
-
#read_paginated(stream, options: {}, middlewares: nil) ⇒ Enumerator
Enumerator will yield PgEventstore::Event.
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
14 15 16 |
# File 'lib/pg_eventstore/client.rb', line 14 def initialize(config) @config = config end |
Instance Attribute Details
#config=(value) ⇒ PgEventstore::Config
10 11 12 |
# File 'lib/pg_eventstore/client.rb', line 10 def config @config end |
Instance Method Details
#append_to_stream(stream, events_or_event, options: {}, middlewares: nil) ⇒ PgEventstore::Event+
Append the event or multiple events to the stream. This operation is atomic, meaning that no other event can be appended by parallel process between the given events.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pg_eventstore/client.rb', line 28 def append_to_stream(stream, events_or_event, options: {}, middlewares: nil) result = Commands::Append.new( Queries.new( partitions: partition_queries, events: event_queries(middlewares(middlewares)), transactions: transaction_queries ) ).call(stream, *events_or_event, options: ) events_or_event.is_a?(Array) ? result : result.first end |
#link_to(stream, events_or_event, options: {}, middlewares: []) ⇒ PgEventstore::Event+
Links event from one stream into another stream. You can later access it by providing :resolve_link_tos option when reading from a stream. Only existing events can be linked.
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pg_eventstore/client.rb', line 155 def link_to(stream, events_or_event, options: {}, middlewares: []) result = Commands::LinkTo.new( Queries.new( partitions: partition_queries, events: event_queries(middlewares(middlewares)), transactions: transaction_queries ) ).call(stream, *events_or_event, options: ) events_or_event.is_a?(Array) ? result : result.first end |
#multiple(&blk) ⇒ Object
Allows you to make several different commands atomic by wrapping then into a block. Order of events, produced by multiple commands, belonging to different streams - is unbreakable. So, if you append event1 to stream1 and event2 to stream2 using this method, then thet appear in the same order in the “all” stream. Example:
PgEventstore.client.multiple do
PgEventstore.client.read(...)
PgEventstore.client.append_to_stream(...)
PgEventstore.client.append_to_stream(...)
end
51 52 53 |
# File 'lib/pg_eventstore/client.rb', line 51 def multiple(&blk) Commands::Multiple.new(Queries.new(transactions: transaction_queries)).call(&blk) end |
#read(stream, options: {}, middlewares: nil) ⇒ Array<PgEventstore::Event>
Read events from the specific stream or from “all” stream.
109 110 111 112 113 |
# File 'lib/pg_eventstore/client.rb', line 109 def read(stream, options: {}, middlewares: nil) Commands::Read. new(Queries.new(partitions: partition_queries, events: event_queries(middlewares(middlewares)))). call(stream, options: { max_count: config.max_count }.merge()) end |
#read_grouped(stream, options: {}, middlewares: nil) ⇒ Array<PgEventstore::Event>
Takes a stream, determines a list of even types in it and returns most recent(or very first - depending on :direction option) events, one of each type. If :event_types filter is provided - uses it instead of automatic event types lookup logic. The result size is almost always less than or equal to event types list size, so passing :max_count option does not make any effect. In case if event of same type appears in different context/stream name - it will be counted as a different event, thus, may appear several times in the result.
137 138 139 140 141 142 |
# File 'lib/pg_eventstore/client.rb', line 137 def read_grouped(stream, options: {}, middlewares: nil) cmd_class = stream.all_stream? ? Commands::AllStreamReadGrouped : Commands::RegularStreamReadGrouped cmd_class. new(Queries.new(partitions: partition_queries, events: event_queries(middlewares(middlewares)))). call(stream, options: ) end |
#read_paginated(stream, options: {}, middlewares: nil) ⇒ Enumerator
Returns enumerator will yield PgEventstore::Event.
120 121 122 123 124 125 |
# File 'lib/pg_eventstore/client.rb', line 120 def read_paginated(stream, options: {}, middlewares: nil) cmd_class = stream.system? ? Commands::SystemStreamReadPaginated : Commands::RegularStreamReadPaginated cmd_class. new(Queries.new(partitions: partition_queries, events: event_queries(middlewares(middlewares)))). call(stream, options: { max_count: config.max_count }.merge()) end |