Skip to main content

Middleware

Command Middlewareā€‹

TypeScript Support

A command middleware function has an associated TypeScript type:

  • Type Name - CommandMiddleware
  • Package - @resolve-js/core

A command middleware function has the following structure:

const commandMiddleware = (next) => (
middlewareContext,
state,
command,
context
) => {
...
state,
return next(middlewareContext, state, command, context)
}

The middleware handler function receives the following arguments:

Parameter NameDescription
middlewareContextContains data that describes the currently processed operation.
stateThe state object built by the aggregate projection.
commandAn object that contains data about the incoming command.
contextThe command context object.

middlewareContextā€‹

A command middleware handler's middlewareContext argument is an object with the following fields:

Field NameDescription
reqStores data that describes the currently processed HTTP request.
resContains the function used to configure the server's response.

Both req and res fields are included only if the client sends the command. If the command is generated on the server (for example, by a saga or API handler), these fields are omitted.

Read Model Projection Middlewareā€‹

TypeScript Support

A read model projection middleware function has an associated TypeScript type:

  • Type Name - ReadModelProjectionMiddleware
  • Package - @resolve-js/core

A read model projection middleware function has the following structure:

const projectionMiddleware = (next) => (
middlewareContext,
store,
event,
context
) => {
...
return next(middlewareContext, store, event, context)
}

The middleware handler function receives the following arguments:

Parameter NameDescription
middlewareContextContains data that describes the currently processed operation.
storeExposes API used to communicate with the read model's persistent data storage.
eventThe incoming event object.
contextThe read model projection context object.

middlewareContextā€‹

A projection middleware handler's middlewareContext argument is an object with the following fields:

Field NameDescription
reqStores data that describes the currently processed HTTP request.
resContains the function used to configure the server's response.
readModelNameThe name of the processed read model.

Read Model Resolver Middlewareā€‹

TypeScript Support

A read model resolver middleware function has an associated TypeScript type:

  • Type Name - ReadModelResolverMiddleware
  • Package - @resolve-js/core

A read model resolver middleware function has the following structure:

const resolverMiddleware = (next) => (
middlewareContext,
store,
params,
context
) => {
...
return next(middlewareContext, store, params, context)
}

The middleware handler function receives the following arguments:

Parameter NameDescription
middlewareContextContains data that describes the currently processed operation.
storeExposes API used to communicate with the read model's persistent data storage.
paramsAn object that contains the request parameters as key-value pairs.
contextThe read model resolver context object.

middlewareContextā€‹

A projection middleware handler's middlewareContext argument is an object with the following fields:

Field NameDescription
reqStores data that describes the currently processed HTTP request.
resContains the function used to configure the server's response.
readModelNameThe name of the processed read model.
resolverNameThe name of the queried resolver.