EventStore
API reference for EventStore
Class: EventStore
Section titled “Class: EventStore”Implementation of the EventStore interface for DeltaBase
Implements
Section titled “Implements”EventStore
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new EventStore(
http
,eventStoreId
):EventStore
Creates a new EventStore client instance
Parameters
Section titled “Parameters”The HTTP client to use for API requests
eventStoreId
Section titled “eventStoreId”string
The ID of the event store to interact with
Returns
Section titled “Returns”EventStore
Methods
Section titled “Methods”aggregateStream()
Section titled “aggregateStream()”aggregateStream<
State
,EventType
>(streamId
,options
):Promise
<AggregateStreamResult
<State
>>
Aggregate events from a stream and compute a state
Type Parameters
Section titled “Type Parameters”State
EventType
Section titled “EventType”EventType
extends Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }>
Parameters
Section titled “Parameters”streamId
Section titled “streamId”string
The ID of the stream to aggregate events from
options
Section titled “options”AggregateStreamOptions
<State
, EventType
>
Configuration options for the aggregation process
Returns
Section titled “Returns”Promise
<AggregateStreamResult
<State
>>
Promise resolving to the aggregation result with the computed state and stream metadata
Example
Section titled “Example”// Define your state type and event typestype UserState = { email: string, isVerified: boolean };type UserEvent = | { type: 'user.created', data: { email: string } } | { type: 'user.verified', data: { verifiedAt: string } };
// Aggregate the stream into a stateconst result = await eventStore.aggregateStream<UserState, UserEvent>( 'user-123', { initialState: () => ({ email: '', isVerified: false }), evolve: (state, event) => { switch (event.type) { case 'user.created': return { ...state, email: event.data.email }; case 'user.verified': return { ...state, isVerified: true }; default: return state; } }, read: { from: 0 } });
Implementation of
Section titled “Implementation of”EventStoreInterface.aggregateStream
appendToStream()
Section titled “appendToStream()”appendToStream<
EventType
>(streamId
,events
,options?
):Promise
<AppendToStreamResult
>
Append events to a stream
Type Parameters
Section titled “Type Parameters”EventType
Section titled “EventType”EventType
extends Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }>
Parameters
Section titled “Parameters”streamId
Section titled “streamId”string
The ID of the stream to append events to
events
Section titled “events”EventType
[]
Array of events to append to the stream
options?
Section titled “options?”Optional parameters for the append operation
Returns
Section titled “Returns”Promise
<AppendToStreamResult
>
Promise resolving to the append result with the next expected version
Throws
Section titled “Throws”When expectedStreamVersion doesn’t match current stream version
Throws
Section titled “Throws”When request validation fails
Throws
Section titled “Throws”When the event store doesn’t exist
Throws
Section titled “Throws”When authentication fails
Example
Section titled “Example”import { isVersionConflictError, isValidationError} from '@delta-base/server';
try { // Append with optimistic concurrency control await eventStore.appendToStream( 'user-123', [{ type: 'user.updated', data: { email: 'updated@example.com' } }], { expectedStreamVersion: 0n } );} catch (error) { if (isVersionConflictError(error)) { console.log(`Version conflict: expected ${error.expectedVersion}, got ${error.currentVersion}`); // Handle concurrency conflict } else if (isValidationError(error)) { console.log('Validation errors:', error.validationErrors); // Handle validation failures } else { throw error; // Re-throw unknown errors }}
Implementation of
Section titled “Implementation of”EventStoreInterface.appendToStream
listStreams()
Section titled “listStreams()”listStreams(
options?
):Promise
<{streams
:string
[];total
:number
; }>
Get a list of stream IDs in an event store
Parameters
Section titled “Parameters”options?
Section titled “options?”Optional parameters for listing streams
limit?
Section titled “limit?”number
Maximum number of stream IDs to return
offset?
Section titled “offset?”number
Number of stream IDs to skip
pattern?
Section titled “pattern?”string
Pattern to match stream IDs (e.g., ‘user-*‘)
Returns
Section titled “Returns”Promise
<{ streams
: string
[]; total
: number
; }>
Promise resolving to an object containing stream IDs and total count
Example
Section titled “Example”// List all streamsconst { streams, total } = await eventStore.listStreams();
// List streams with paginationconst { streams, total } = await eventStore.listStreams({ limit: 50, offset: 100});
// List streams matching a patternconst { streams, total } = await eventStore.listStreams({ pattern: 'user-*'});
queryEvents()
Section titled “queryEvents()”queryEvents<
EventType
>(options
):Promise
<QueryEventsResult
<EventType
>>
Query events with flexible filtering options
Type Parameters
Section titled “Type Parameters”EventType
Section titled “EventType”EventType
extends Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }> = Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }>
Parameters
Section titled “Parameters”options
Section titled “options”QueryEventsOptions
= {}
Query parameters for filtering events
Returns
Section titled “Returns”Promise
<QueryEventsResult
<EventType
>>
Promise resolving to the query result with events and pagination info
Example
Section titled “Example”// Query all events of a specific typeconst result = await eventStore.queryEvents({ type: 'user.created'});
// Query events with paginationconst result = await eventStore.queryEvents({ limit: 20, offset: 40, includeCount: true});
// Query events within a time rangeconst result = await eventStore.queryEvents({ fromDate: '2023-01-01T00:00:00Z', toDate: '2023-01-31T23:59:59Z'});
Implementation of
Section titled “Implementation of”EventStoreInterface.queryEvents
queryStreamEvents()
Section titled “queryStreamEvents()”queryStreamEvents<
EventType
>(streamId
,options
):Promise
<QueryEventsResult
<EventType
>>
Query events for a specific stream with filtering options
Type Parameters
Section titled “Type Parameters”EventType
Section titled “EventType”EventType
extends Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }> = Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }>
Parameters
Section titled “Parameters”streamId
Section titled “streamId”string
The ID of the stream to query events from
options
Section titled “options”Omit
<QueryEventsOptions
, "streamId"
> = {}
Query parameters for filtering events
Returns
Section titled “Returns”Promise
<QueryEventsResult
<EventType
>>
Promise resolving to the query result with events and pagination info
Example
Section titled “Example”// Query events for a specific streamconst result = await eventStore.queryStreamEvents('user-123', { type: 'user.updated'});
queryStreams()
Section titled “queryStreams()”queryStreams(
options
):Promise
<QueryStreamsResult
>
Query streams with filtering options
Parameters
Section titled “Parameters”options
Section titled “options”QueryStreamsOptions
= {}
Query parameters for filtering streams
Returns
Section titled “Returns”Promise
<QueryStreamsResult
>
Promise resolving to the query result with streams and pagination info
Example
Section titled “Example”// Query all streamsconst result = await eventStore.queryStreams();
// Query streams by typeconst result = await eventStore.queryStreams({ streamType: 'user'});
// Query streams with a pattern matchconst result = await eventStore.queryStreams({ streamIdPattern: 'user-%'});
queryStreamsByType()
Section titled “queryStreamsByType()”queryStreamsByType(
streamType
,options
):Promise
<QueryStreamsResult
>
Query streams of a specific type with filtering options
Parameters
Section titled “Parameters”streamType
Section titled “streamType”string
The stream type to filter by
options
Section titled “options”Omit
<QueryStreamsOptions
, "streamType"
> = {}
Query parameters for filtering streams
Returns
Section titled “Returns”Promise
<QueryStreamsResult
>
Promise resolving to the query result with streams and pagination info
Example
Section titled “Example”// Query all user streamsconst result = await eventStore.queryStreamsByType('user');
// Query user streams with paginationconst result = await eventStore.queryStreamsByType('user', { limit: 20, offset: 0});
readStream()
Section titled “readStream()”readStream<
EventType
>(streamId
,options?
):Promise
<ReadStreamResult
<EventType
>>
Read events from a stream
Type Parameters
Section titled “Type Parameters”EventType
Section titled “EventType”EventType
extends Readonly
<{ data
: EventData
; metadata?
: PlatformEventMetadata
; type
: string
; }>
Parameters
Section titled “Parameters”streamId
Section titled “streamId”string
The ID of the stream to read events from
options?
Section titled “options?”The options for reading events
Returns
Section titled “Returns”Promise
<ReadStreamResult
<EventType
>>
Promise resolving to the read result containing events and stream metadata
Example
Section titled “Example”// Read all events from a streamconst result = await eventStore.readStream('user-123');
// Read events with a specific starting positionconst result = await eventStore.readStream('user-123', { from: 5 });
// Read a specific range of eventsconst result = await eventStore.readStream('user-123', { from: 5, to: 10 });
// Read a limited number of eventsconst result = await eventStore.readStream('user-123', { maxCount: 100 });
Implementation of
Section titled “Implementation of”EventStoreInterface.readStream