[fabric/domain] Refactor event store and event stream interfaces
This commit is contained in:
parent
4fff9f91f5
commit
6919a819b6
@ -1,36 +1,26 @@
|
||||
import { AsyncResult, MaybePromise, PosixDate } from "@fabric/core";
|
||||
import { AsyncResult, PosixDate } from "@fabric/core";
|
||||
import { StoreQueryError } from "../errors/query-error.js";
|
||||
import { UUID } from "../types/uuid.js";
|
||||
import { Event, StoredEvent } from "./event.js";
|
||||
import { EventsFromStream, EventStream } from "./event-stream.js";
|
||||
import { StoredEvent } from "./stored-event.js";
|
||||
|
||||
export interface EventStore<TEvent extends Event = Event> {
|
||||
getStream<TEventStreamEvent extends TEvent>(
|
||||
streamId: UUID,
|
||||
): AsyncResult<EventStream<TEventStreamEvent>, StoreQueryError>;
|
||||
|
||||
appendToStream<TEvent extends Event>(
|
||||
streamId: UUID,
|
||||
events: TEvent,
|
||||
): AsyncResult<void, StoreQueryError>;
|
||||
}
|
||||
|
||||
export interface EventStream<TEvent extends Event = Event> {
|
||||
getCurrentVersion(): bigint;
|
||||
|
||||
append(events: TEvent): AsyncResult<StoredEvent<TEvent>, StoreQueryError>;
|
||||
|
||||
subscribe(callback: (event: StoredEvent<TEvent>) => MaybePromise<void>): void;
|
||||
|
||||
getEvents(
|
||||
opts?: EventFilterOptions,
|
||||
): AsyncResult<StoredEvent<TEvent>[], StoreQueryError>;
|
||||
export interface EventStore<TEventStream extends EventStream> {
|
||||
/**
|
||||
* Store a new event in the event store.
|
||||
*/
|
||||
append<
|
||||
TStreamKey extends TEventStream["name"],
|
||||
T extends EventsFromStream<TEventStream, TStreamKey>,
|
||||
>(
|
||||
streamName: TStreamKey,
|
||||
event: T,
|
||||
): AsyncResult<StoredEvent<T>, StoreQueryError>;
|
||||
}
|
||||
|
||||
export interface EventFilterOptions {
|
||||
fromDate?: PosixDate;
|
||||
toDate?: PosixDate;
|
||||
fromVersion?: number;
|
||||
toVersion?: number;
|
||||
fromVersion?: bigint;
|
||||
toVersion?: bigint;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
16
packages/fabric/domain/src/events/event-stream.ts
Normal file
16
packages/fabric/domain/src/events/event-stream.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { AsyncResult } from "@fabric/core";
|
||||
import { UUID } from "../types/uuid.js";
|
||||
import { Event } from "./event.js";
|
||||
import { StoredEvent } from "./stored-event.js";
|
||||
|
||||
export interface EventStream<
|
||||
TName extends string = string,
|
||||
TEvent extends Event = Event,
|
||||
> {
|
||||
id: UUID;
|
||||
name: TName;
|
||||
append<T extends TEvent>(event: Event): AsyncResult<StoredEvent<T>>;
|
||||
}
|
||||
|
||||
export type EventsFromStream<T extends EventStream, TKey extends string> =
|
||||
T extends EventStream<TKey, infer TEvent> ? TEvent : never;
|
||||
@ -1,20 +1,12 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { PosixDate, TaggedVariant } from "@fabric/core";
|
||||
import { UUID } from "../types/uuid.js";
|
||||
|
||||
/**
|
||||
* An event is a tagged variant with a payload and a timestamp.
|
||||
*/
|
||||
export interface Event<TTag extends string = string, TPayload = any>
|
||||
extends TaggedVariant<TTag> {
|
||||
streamId: UUID;
|
||||
payload: TPayload;
|
||||
export interface Event<TTag extends string = string, TPayload = any> {
|
||||
readonly type: TTag;
|
||||
readonly id: UUID;
|
||||
readonly streamId: UUID;
|
||||
readonly payload: TPayload;
|
||||
}
|
||||
|
||||
/**
|
||||
* A stored event is an inmutable event, already stored, with it's version in the stream and timestamp.
|
||||
*/
|
||||
export type StoredEvent<TEvent extends Event> = Readonly<TEvent> & {
|
||||
readonly version: number;
|
||||
readonly timestamp: PosixDate;
|
||||
};
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
export * from "./event-store.js";
|
||||
export * from "./event-stream.js";
|
||||
export * from "./event.js";
|
||||
export * from "./stored-event.js";
|
||||
|
||||
10
packages/fabric/domain/src/events/stored-event.ts
Normal file
10
packages/fabric/domain/src/events/stored-event.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { PosixDate } from "@fabric/core";
|
||||
import { Event } from "./event.js";
|
||||
|
||||
/**
|
||||
* A stored event is an inmutable event, already stored, with it's version in the stream and timestamp.
|
||||
*/
|
||||
export type StoredEvent<TEvent extends Event> = TEvent & {
|
||||
readonly version: bigint;
|
||||
readonly timestamp: PosixDate;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user