MessageActor

MessageActor

Class that extends Actor with more specific handling of Domain Commands|Events to act upon the Model. It takes 2 additional parameters than the base Actor, the Command and Event JSON Schema definitions.

Primary use case(s) are:

  • process CQRS Commands to generate Events to apply to a Model in perform method

Constructor

new MessageActor(eventSchema, commandSchemaopt, modelSchemaopt, repositoryopt)

Properties:
Name Type Description
repository any A reference to a storage layer client of your choosing or undefined.
Parameters:
Name Type Attributes Description
eventSchema Schema The instance of the Actor's associated Event JSON Schema definition.
commandSchema Schema <optional>
The optional instance of the Actor's associated Command JSON Schema definition.
modelSchema Schema <optional>
The instance of the associated Model's JSON Schema definition.
repository any <optional>
An optional reference to a storage layer client of your choosing.
Examples

A Command example MessageActor class. It is meant to be wrapped with one of the microservice types (Producer, Consumer, Stream Processor). Actors wrapped by each of the previously mentioned types are passed references to the centralized log store when perform and replay methods are called.

import { MessageActor, Schema } from 'hive-io'

import ExampleSchema from '../schemas/ExampleSchema.json'
import EventSchema from '../schemas/EventSchema.json'
import CommandSchema from '../schemas/CommandSchema.json'

class CommandActor extends MessageActor {
  async perform (model, action) {
    // Do something interesting with the data
  }
}
// Proxy<CommandActor>
export default new Proxy(CommandActor, {
  construct: async function (CommandActor, argsList) {
    const example = await new Schema(ExampleSchema)
    const event = await new Schema(EventSchema)
    const command = await new Schema(CommandSchema)

    return new CommandActor(event, command, example, argsList[0])
  }
})

An Event example MessageActor class. It is meant to be wrapped with one of the microservice types (Producer, Consumer, Stream Processor). Actors wrapped by each of the previously mentioned types are passed references to the centralized log store when perform and replay methods are called.

import { MessageActor, Schema } from 'hive-io'

import ExampleSchema from '../schemas/ExampleSchema.json'
import EventSchema from '../schemas/EventSchema.json'

class EventActor extends MessageActor {
  async perform (model, action) {
    // Do something interesting with the data
  }
}
// Proxy<EventActor>
export default new Proxy(EventActor, {
  construct: async function (EventActor, argsList) {
    const example = await new Schema(ExampleSchema)
    const event = await new Schema(EventSchema)

    return new EventActor(event, undefined, example, argsList[0])
  }
})

Methods

assign(model, payloadopt, metaopt) → {Object}

Method used to generate the materialized view of a specified model from specified payload. It also handles data version specified in the meta Object literal to optionally assist in optimisted concurrency techniques if set.
Parameters:
Name Type Attributes Default Description
model Object The instance of a model to receive new data.
payload Object <optional>
{} The payload to apply to the model.
meta Object <optional>
The metadata associated with the payload being assigned.
Returns:
Type:
Object
The updated instance of the model.

(async) perform(model, actionopt) → {Object}

Method that performs the specified Command in the action to the specified model.
Parameters:
Name Type Attributes Description
model Object An instance of the model associated with the received data generated from replay.
action Object <optional>
An optional FSA Object literal containing the Model's type and optional meta and payload.
Returns:
Type:
Object
An Object literal containing the latest materialized view of the model and the immutable instances of the command and event.