DocumentationReferenceCreating the write modelDefining events

Defining events

An event updates the state of the aggregate once it has been published. So, a function that is defined inside of the events object can be seen as an event handler that reacts to a particular event and updates the aggregate's state.

To define an event add an appropriately named function to the events object of the aggregate the event refers to. This function receives two parameters: the aggregate instance, and the event.

Inside of the function, add code that modifies the aggregate state. For that use the setState function of the aggregate.

E.g., to handle an issued event and set the isIssued property to true, use the following code:

const events = {
  issued (invoice, event) {
      isIssued: true

For a detailled list of an event's properties, see the data structure of events.

Reserved event names

Do not name an event transferredOwnership or authorized, since these are reserved names.

Accessing the event data

To set the aggregate state you may need to access the event data. For that, use the event.data property.

E.g., to set the requiresAttention property depending on the amount that is given in the issued event, use the following code:

  requiresAttention: event.data.amount > 2500