default

Examples

Basic usage with Task semantics

import { EventLog, createInMemory } from "@anabranch/eventlog";

const connector = createInMemory();
const log = await EventLog.connect(connector).run();

const eventId = await log.append("users", { action: "created", userId: 123 }).run();
const events = await log.list("users").run();

Consuming events as a stream with auto-commit

const connector = createInMemory();
const log = await EventLog.connect(connector).run();

const { successes, errors } = await log
  .consume("users", "my-processor")
  .withConcurrency(5)
  .map(async (batch) => {
    for (const event of batch.events) {
      await processEvent(event.data);
    }
  })
  .partition();

Classes

c
EventLogCloseFailed(
message: string,
cause?: unknown
)

Error thrown when closing an event log connection fails.

c
EventLogConnectionFailed(
message: string,
cause?: unknown
)

Error thrown when an event log connection cannot be established.

c
Source<T, E>(
resultSource: () => AsyncGenerator<Result<T, E>>,
concurrency?: number,
bufferSize?: number
)

The entry point for creating a Stream. Wraps an async generator so that yielded values become success results and any thrown error becomes a single error result.

c
Task<T, E>(task: (signal?: AbortSignal) => Promise<T>)

A single async task with error-aware utilities like retries and timeouts.

Functions

Interfaces

I
AppendOptions

Options for appending events.

I
ConsumeOptions

Options for consuming events.

  • batchSize: number

    Maximum number of events per batch. Defaults to adapter-specific value.

  • bufferSize: number

    Maximum number of batches to buffer. Defaults to adapter-specific value. When the buffer is full, new batches will be dropped and onError will be called with an EventLogConsumeFailed error.

  • cursor: Cursor | null

    Cursor to resume from. If null, starts from the beginning.

  • signal: AbortSignal

    Abort signal to cancel consumption.

I
Event

A single event in the event log.

I
EventBatch

A batch of events delivered to a consumer.

I
EventLogConnector

Factory for creating event log connections.

I
EventLogOptions

Configuration options for event log implementations.

I
InMemoryConnector

Creates an in-memory event log connector for testing and development.

I
InMemoryOptions

Configuration options for in-memory event log.

I
Stream

A TypeScript library that provides a powerful and flexible way to handle errors in asynchronous streams. It allows you to collect and manage errors alongside successful values in a stream, enabling you to process data while gracefully handling any issues that may arise.