ViewModel
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.
Classes
Section titled “Classes”UnknownViewModel
Section titled “UnknownViewModel”Defined in: src/ViewModel.ts:40
Extends
Section titled “Extends”object&YieldableError<this>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new UnknownViewModel(…
args):UnknownViewModel
Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Schema.d.ts:10979
Parameters
Section titled “Parameters”…[props: object, options?: MakeOptions]
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Schema.TaggedError<UnknownViewModel>()("UnknownViewModel", { name: Schema.String }).constructor
Properties
Section titled “Properties”name:
string=Schema.String
Defined in: src/ViewModel.ts:41
Inherited from
Section titled “Inherited from”Schema.TaggedError<UnknownViewModel>()("UnknownViewModel", { name: Schema.String }).name
ViewModelError
Section titled “ViewModelError”Defined in: src/ViewModel.ts:44
Extends
Section titled “Extends”object&YieldableError<this>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ViewModelError(…
args):ViewModelError
Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Schema.d.ts:10979
Parameters
Section titled “Parameters”…[props: object, options?: MakeOptions]
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”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
Properties
Section titled “Properties”cause:
unknown
Defined in: src/ViewModel.ts:48
Inherited from
Section titled “Inherited from”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
position
Section titled “position”
readonlyposition:number=Schema.Int
Defined in: src/ViewModel.ts:47
The event being processed, 0 when the failure happened outside any event.
Inherited from
Section titled “Inherited from”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
viewModel
Section titled “viewModel”
readonlyviewModel:string=Schema.String
Defined in: src/ViewModel.ts:45
Inherited from
Section titled “Inherited from”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
ViewModels
Section titled “ViewModels”Defined in: src/ViewModel.ts:206
Extends
Section titled “Extends”Shape<"effect-cqrs/ViewModels", {status:Effect<readonlyStatus[]>;caughtUp(position?):Effect<void,EventStoreError>;parked(viewModel?):Effect<readonlyobject[],EventStoreError|UnknownViewModel>;replay(options):Effect<ReplayResult,UnknownViewModel|ViewModelError>;retryParked(options):Effect<RetryParkedResult,EventStoreError|UnknownViewModel|ViewModelError>; },this>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ViewModels(
_):ViewModels
Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Context.d.ts:121
Parameters
Section titled “Parameters”never
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”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
Interfaces
Section titled “Interfaces”ReplayOptions
Section titled “ReplayOptions”Defined in: src/ViewModel.ts:171
Properties
Section titled “Properties”finishTime?
Section titled “finishTime?”
readonlyoptionalfinishTime?:Utc
Defined in: src/ViewModel.ts:174
startTime?
Section titled “startTime?”
readonlyoptionalstartTime?:Utc
Defined in: src/ViewModel.ts:173
viewModels
Section titled “viewModels”
readonlyviewModels: readonlystring[]
Defined in: src/ViewModel.ts:172
ReplayResult
Section titled “ReplayResult”Defined in: src/ViewModel.ts:177
Properties
Section titled “Properties”eventCount
Section titled “eventCount”
readonlyeventCount:number
Defined in: src/ViewModel.ts:179
viewModels
Section titled “viewModels”
readonlyviewModels: readonlystring[]
Defined in: src/ViewModel.ts:178
RetryParkedOptions
Section titled “RetryParkedOptions”Defined in: src/ViewModel.ts:193
Properties
Section titled “Properties”positions?
Section titled “positions?”
readonlyoptionalpositions?: readonlynumber[]
Defined in: src/ViewModel.ts:196
The positions of the events to retry. Defaults to every event the view model parked.
viewModel
Section titled “viewModel”
readonlyviewModel:string
Defined in: src/ViewModel.ts:194
RetryParkedResult
Section titled “RetryParkedResult”Defined in: src/ViewModel.ts:199
Properties
Section titled “Properties”failed
Section titled “failed”
readonlyfailed: readonlyobject[]
Defined in: src/ViewModel.ts:203
The events that failed again, as they are parked now.
succeeded
Section titled “succeeded”
readonlysucceeded: readonlynumber[]
Defined in: src/ViewModel.ts:201
The positions of the events that were handled, and are no longer parked.
Status
Section titled “Status”Defined in: src/ViewModel.ts:182
Properties
Section titled “Properties”holdsLease
Section titled “holdsLease”
readonlyholdsLease:boolean
Defined in: src/ViewModel.ts:190
Whether this process holds the view model’s lease, and so runs it. See Leases.
lastError
Section titled “lastError”
readonlylastError:Option<ViewModelError>
Defined in: src/ViewModel.ts:186
The failure the view model is retrying, while it is stalled on one.
readonlyname:string
Defined in: src/ViewModel.ts:183
parked
Section titled “parked”
readonlyparked:number
Defined in: src/ViewModel.ts:188
How many events the view model parked and skips, see park.
position
Section titled “position”
readonlyposition:number
Defined in: src/ViewModel.ts:184
ViewModel
Section titled “ViewModel”Defined in: src/ViewModel.ts:66
Type Parameters
Section titled “Type Parameters”Name extends string
E
R
Properties
Section titled “Properties”aggregates
Section titled “aggregates”
readonlyaggregates: readonlyAny[]
Defined in: src/ViewModel.ts:68
dispose
Section titled “dispose”
readonlydispose:Effect<void,E,R>
Defined in: src/ViewModel.ts:71
Clears everything the view model stored, before a replay rebuilds it.
handlers
Section titled “handlers”
readonlyhandlers:Record<string, (event,meta) =>Effect.Effect<unknown,E,R>>
Defined in: src/ViewModel.ts:69
readonlyname:Name
Defined in: src/ViewModel.ts:67
onFailure?
Section titled “onFailure?”
readonlyoptionalonFailure?:FailurePolicy
Defined in: src/ViewModel.ts:75
What happens to an event its handler keeps failing on. Defaults to stall.
onForget?
Section titled “onForget?”
readonlyoptionalonForget?: (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.
Parameters
Section titled “Parameters”subject
Section titled “subject”string
Returns
Section titled “Returns”Effect<unknown, E, R>
transaction?
Section titled “transaction?”
readonlyoptionaltransaction?:Transaction<R>
Defined in: src/ViewModel.ts:77
Runs each batch of handlers together with the checkpoint they reach, see sqlTransaction.
Type Aliases
Section titled “Type Aliases”Any =
ViewModel<string,any,any>
Defined in: src/ViewModel.ts:114
Context
Section titled “Context”Context<
V> =VextendsViewModel<any,any, infer R> ?R:never
Defined in: src/ViewModel.ts:116
Type Parameters
Section titled “Type Parameters”V extends Any
FailurePolicy
Section titled “FailurePolicy”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.
Stallretries it forever, with a growing delay, and nothing after it is processed in the meantime. Nothing is ever skipped.Parkretries it until it has failedattemptstimes, then records it inParkedEventsand moves on.ViewModels.retryParkedhandles it again later.
Handlers
Section titled “Handlers”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
Type Parameters
Section titled “Type Parameters”Event extends Tagged
Transaction
Section titled “Transaction”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.
Type Parameters
Section titled “Type Parameters”R
Parameters
Section titled “Parameters”effect
Section titled “effect”Effect.Effect<void, ViewModelError>
Returns
Section titled “Returns”Effect.Effect<void, unknown, R>
Variables
Section titled “Variables”MetricNames
Section titled “MetricNames”
constMetricNames: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.
Type Declaration
Section titled “Type Declaration”parkedEvents
Section titled “parkedEvents”
readonlyparkedEvents:"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.
sqlTransaction
Section titled “sqlTransaction”
constsqlTransaction: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.
conststall:FailurePolicy
Defined in: src/ViewModel.ts:106
Retries a failing event until it succeeds, holding everything after it back. The default.
Functions
Section titled “Functions”layer()
Section titled “layer()”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.
Type Parameters
Section titled “Type Parameters”VMs extends readonly Any[]
Parameters
Section titled “Parameters”viewModels
Section titled “viewModels”VMs
Returns
Section titled “Returns”Layer<ViewModels, EventStoreError, EventStore | Checkpoints | Context<VMs[number]>>
make()
Section titled “make()”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.
Type Parameters
Section titled “Type Parameters”Name extends string
Aggregates
Section titled “Aggregates”Aggregates extends readonly Any[]
H extends Handlers<Event<Aggregates[number]>>
E = never
R = never
FE = never
FR = never
TR = never
Parameters
Section titled “Parameters”options
Section titled “options”aggregates
Section titled “aggregates”Aggregates
dispose
Section titled “dispose”Effect<void, E, R>
handlers
Section titled “handlers”H
Name
onFailure?
Section titled “onFailure?”onForget?
Section titled “onForget?”(subject, meta) => Effect<unknown, FE, FR>
transaction?
Section titled “transaction?”Transaction<TR>
Returns
Section titled “Returns”ViewModel<Name, E | FE | HandlerError<H>, R | FR | TR | HandlerContext<H>>
park()
Section titled “park()”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.
Parameters
Section titled “Parameters”options
Section titled “options”attempts
Section titled “attempts”number