Skip to content

The read side: projections of events into whatever storage a query needs.

Moleculer-cqrs pushes events to view models over the broker, so an event sent while a service is down is lost until someone replays. Here each view model pulls from the store from its own checkpoint, so it can’t miss anything, and delivery is at least once: a handler can see an event again after a crash and should be idempotent, unless the view model has a transaction.

Defined in: src/ViewModel.ts:40

  • object & YieldableError<this>

new UnknownViewModel(…args): UnknownViewModel

Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Schema.d.ts:10979

…[props: object, options?: MakeOptions]

UnknownViewModel

Schema.TaggedError<UnknownViewModel>()("UnknownViewModel", { name: Schema.String }).constructor

name: string = Schema.String

Defined in: src/ViewModel.ts:41

Schema.TaggedError<UnknownViewModel>()("UnknownViewModel", { name: Schema.String }).name


Defined in: src/ViewModel.ts:44

  • object & YieldableError<this>

new ViewModelError(…args): ViewModelError

Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Schema.d.ts:10979

…[props: object, options?: MakeOptions]

ViewModelError

Schema.TaggedError<ViewModelError>()("ViewModelError", { viewModel: Schema.String, /** The event being processed, `0` when the failure happened outside any event. */ position: Schema.Int, cause: Schema.Defect() }).constructor

cause: unknown

Defined in: src/ViewModel.ts:48

Schema.TaggedError<ViewModelError>()("ViewModelError", { viewModel: Schema.String, /** The event being processed, `0` when the failure happened outside any event. */ position: Schema.Int, cause: Schema.Defect() }).cause

readonly position: number = Schema.Int

Defined in: src/ViewModel.ts:47

The event being processed, 0 when the failure happened outside any event.

Schema.TaggedError<ViewModelError>()("ViewModelError", { viewModel: Schema.String, /** The event being processed, `0` when the failure happened outside any event. */ position: Schema.Int, cause: Schema.Defect() }).position

readonly viewModel: string = Schema.String

Defined in: src/ViewModel.ts:45

Schema.TaggedError<ViewModelError>()("ViewModelError", { viewModel: Schema.String, /** The event being processed, `0` when the failure happened outside any event. */ position: Schema.Int, cause: Schema.Defect() }).viewModel


Defined in: src/ViewModel.ts:206

new ViewModels(_): ViewModels

Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Context.d.ts:121

never

ViewModels

Context.Service<ViewModels, { /** * Disposes the named view models and rebuilds them from their events. A time * window limits which events are replayed: the view model then resumes from * the present and events outside the window stay out of it. */ replay(options: ReplayOptions): Effect.Effect<ReplayResult, UnknownViewModel | ViewModelError> /** * Waits until every view model has processed `position`, defaulting to the * latest event. Pair it with `CommandResult.position` to read your own writes. * A view model stalled on a failure doesn’t catch up, so consider a timeout. * View models that another process holds are not waited for. */ caughtUp(position?: number): Effect.Effect<void, EventStoreError> readonly status: Effect.Effect<ReadonlyArray<Status>> /** The events the view models of this layer parked, or those of one of them. */ parked(viewModel?: string): Effect.Effect<ReadonlyArray<ParkedEvents.ParkedEvent>, UnknownViewModel | EventStoreError> /** * Handles parked events again, in order of position. Those that succeed are no * longer parked, and the others stay parked with the new failure. */ retryParked( options: RetryParkedOptions ): Effect.Effect<RetryParkedResult, UnknownViewModel | ViewModelError | EventStoreError> }>()(“effect-cqrs/ViewModels”).constructor

Defined in: src/ViewModel.ts:171

readonly optional finishTime?: Utc

Defined in: src/ViewModel.ts:174

readonly optional startTime?: Utc

Defined in: src/ViewModel.ts:173

readonly viewModels: readonly string[]

Defined in: src/ViewModel.ts:172


Defined in: src/ViewModel.ts:177

readonly eventCount: number

Defined in: src/ViewModel.ts:179

readonly viewModels: readonly string[]

Defined in: src/ViewModel.ts:178


Defined in: src/ViewModel.ts:193

readonly optional positions?: readonly number[]

Defined in: src/ViewModel.ts:196

The positions of the events to retry. Defaults to every event the view model parked.

readonly viewModel: string

Defined in: src/ViewModel.ts:194


Defined in: src/ViewModel.ts:199

readonly failed: readonly object[]

Defined in: src/ViewModel.ts:203

The events that failed again, as they are parked now.

readonly succeeded: readonly number[]

Defined in: src/ViewModel.ts:201

The positions of the events that were handled, and are no longer parked.


Defined in: src/ViewModel.ts:182

readonly holdsLease: boolean

Defined in: src/ViewModel.ts:190

Whether this process holds the view model’s lease, and so runs it. See Leases.

readonly lastError: Option<ViewModelError>

Defined in: src/ViewModel.ts:186

The failure the view model is retrying, while it is stalled on one.

readonly name: string

Defined in: src/ViewModel.ts:183

readonly parked: number

Defined in: src/ViewModel.ts:188

How many events the view model parked and skips, see park.

readonly position: number

Defined in: src/ViewModel.ts:184


Defined in: src/ViewModel.ts:66

Name extends string

E

R

readonly aggregates: readonly Any[]

Defined in: src/ViewModel.ts:68

readonly dispose: Effect<void, E, R>

Defined in: src/ViewModel.ts:71

Clears everything the view model stored, before a replay rebuilds it.

readonly handlers: Record<string, (event, meta) => Effect.Effect<unknown, E, R>>

Defined in: src/ViewModel.ts:69

readonly name: Name

Defined in: src/ViewModel.ts:67

readonly optional onFailure?: FailurePolicy

Defined in: src/ViewModel.ts:75

What happens to an event its handler keeps failing on. Defaults to stall.

readonly optional onForget?: (subject, meta) => Effect<unknown, E, R>

Defined in: src/ViewModel.ts:73

Erases what the view model stored about a forgotten subject, see PersonalData.forget.

string

EventMeta

Effect<unknown, E, R>

readonly optional transaction?: Transaction<R>

Defined in: src/ViewModel.ts:77

Runs each batch of handlers together with the checkpoint they reach, see sqlTransaction.

Any = ViewModel<string, any, any>

Defined in: src/ViewModel.ts:114


Context<V> = V extends ViewModel<any, any, infer R> ? R : never

Defined in: src/ViewModel.ts:116

V extends Any


FailurePolicy = { _tag: "Stall"; } | { _tag: "Park"; attempts: number; }

Defined in: src/ViewModel.ts:101

What a view model does about an event whose handler keeps failing.

  • Stall retries it forever, with a growing delay, and nothing after it is processed in the meantime. Nothing is ever skipped.
  • Park retries it until it has failed attempts times, then records it in ParkedEvents and moves on. ViewModels.retryParked handles it again later.

Handlers<Event> = { readonly [Tag in Event["_tag"]]?: (event: Extract<Event, { _tag: Tag }>, meta: EventMeta) => Effect.Effect<unknown, any, any> }

Defined in: src/ViewModel.ts:51

Event extends Tagged


Transaction<R> = (effect) => Effect.Effect<void, unknown, R>

Defined in: src/ViewModel.ts:84

Wraps a batch of handlers and the checkpoint after them, so that they commit or roll back together.

R

Effect.Effect<void, ViewModelError>

Effect.Effect<void, unknown, R>

const MetricNames: object

Defined in: src/ViewModel.ts:236

The names of the metrics view models record through Effect’s Metric module. Each series carries a view_model attribute with the view model’s name.

readonly parkedEvents: "cqrs_view_model_parked_events_total" = "cqrs_view_model_parked_events_total"

Counter: events a view model parked after its handler kept failing on them.


const sqlTransaction: Transaction<SqlClient.SqlClient>

Defined in: src/ViewModel.ts:90

A transaction of the SqlClient in context. The read model and the checkpoints have to use that same client for their writes to join it.


const stall: FailurePolicy

Defined in: src/ViewModel.ts:106

Retries a failing event until it succeeds, holding everything after it back. The default.

layer<VMs>(viewModels): Layer<ViewModels, EventStoreError, EventStore | Checkpoints | Context<VMs[number]>>

Defined in: src/ViewModel.ts:723

Starts the view models in the background for as long as the layer lives.

Each view model runs where its lease is held, see Leases: by default in every process, which suits a single one. Events that view models park go to the ParkedEvents in context, or to memory without one.

VMs extends readonly Any[]

VMs

Layer<ViewModels, EventStoreError, EventStore | Checkpoints | Context<VMs[number]>>


make<Name, Aggregates, H, E, R, FE, FR, TR>(options): ViewModel<Name, E | FE | HandlerError<H>, R | FR | TR | HandlerContext<H>>

Defined in: src/ViewModel.ts:130

Handlers are keyed by event tag, and events without one are skipped.

A view model that keeps personal data handles onForget, which runs when PersonalData.forget records that a subject was forgotten, in order with the events around it. Events the view model reads afterwards already carry the replacements, so it only has to erase what it stored before.

onFailure decides what happens to an event that keeps failing, see FailurePolicy. With transaction, each batch of handlers runs in one transaction with the checkpoint it reaches, so a crash never repeats an event.

Name extends string

Aggregates extends readonly Any[]

H extends Handlers<Event<Aggregates[number]>>

E = never

R = never

FE = never

FR = never

TR = never

Aggregates

Effect<void, E, R>

H

Name

FailurePolicy

(subject, meta) => Effect<unknown, FE, FR>

Transaction<TR>

ViewModel<Name, E | FE | HandlerError<H>, R | FR | TR | HandlerContext<H>>


park(options): FailurePolicy

Defined in: src/ViewModel.ts:109

Gives up on an event after it failed attempts times, and goes on with the next ones.

number

FailurePolicy