Spear.append_batch_stream
append_batch_stream
, go back to Spear module for more information.
Specs
append_batch_stream( batch_stream :: Enumerable.t(), connection :: Spear.Connection.t() ) :: Enumerable.t()
A convenience wrapper around append_batch/5
for transforming a stream
into a batch append operation.
The append_batch/5
function provides fine-grained control over the
batch append feature. This function transforms an input stream of batches
to apply the append_batch/5
on all of them, making sure to clean up
the request once the batch is finished.
The expected batch_stream
is an enumerable with each element follows the
format
{stream_name :: String.t(), events :: [Spear.Event.t()]}
# or
{stream_name :: String.t(), events :: [Spear.Event.t()], opts :: Keyword.t()}
Where opts
can be any option below. The options below are applied to
each call to append_batch/5
when provided, except :credentials
which is only applied when specified on the first batch.
The resulting stream must be run (with an Enum
function or Stream.run/1
).
Each element is mapped to the acknowledgement Spear.BatchAppendResult.t/0
responses for each batch attempting to be appended.
Options
:expect
- (default::any
) the expectation to set on the status of the stream. The write will fail if the expectation fails. SeeSpear.ExpectationViolation
for more information about expectations.:raw?
- (default:false
) a boolean which controls whether messages emitted to the:send_ack_to
process are decoded fromSpear.Records.Streams.batch_append_resp/0
records. Spear preserves most of the information when decoding the record into theSpear.BatchAppendResult.t/0
struct, but it discards duplicated information such as stream name and expected revision. Setting the:raw?
flag allows one to decode the record however they wish.:credentials
- (default:nil
) a two-tuple{username, password}
to use as credentials for the request. This option overrides any credentials set in the connection configuration, if present. See the Security guide for more details.:timeout
- (default:5_000
) the timeout for the initial call to open the batch request. After the first batch, this argument instead controls how long eachappend_batch/5
operation will await an acknowledgement.
Examples
batch_stream
|> Spear.append_batch_stream(conn)
|> Enum.reduce_while(:ok, fn ack, _acc ->
case ack.result do
:ok -> {:cont, :ok}
{:error, reason} -> {:halt, {:error, reason}}
end
end)
#=> :ok