JSPM

resolve-command

0.0.25
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 116
  • Score
    100M100P100Q80110F
  • License MIT

This package creates a function to execute a command.

Package Exports

  • resolve-command

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resolve-command) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

📢 resolve-command npm version

Provides a function to handle a command and send the generated event to an event store based on definitions of aggregates and their commands.

Usage

When initializing a command, pass the following arguments:

After the command is initialized, you get a function that is used to send an event to the event store. It receives two arguments:

  • command An object with the following fields:

    • aggregateId - unique id of aggregate
    • aggregateName - name of aggregate that has to handle command
    • type - command type

    A command may also have some additional payload.

  • getJwt Callback for retrieve actual client state stored in verified JWT token.

Example

import commandHandler from 'resolve-command'
import createEsStorage from 'resolve-storage-memory'
import createBusDriver from 'resolve-bus-memory'
import createEventStore from 'resolve-es'

const aggregates = [{
  name: 'User',
  initialState: {},
  eventHandlers: {
    UserCreated: (state, { name, email}) => ({
      ...state,
      name,
      email
    })
  },
  commands: {
    create: (state, { aggregateId, name, email}) => ({
      type: 'UserCreated',
      aggregateId,
      name,
      email
    })
  }
}]

const eventStore = createEventStore({ storage: createEsStorage(), bus: createBusDriver() })

eventStore.onEvent(['UserCreated'], event =>
  console.log('Event emitted', event)
)

const execute = commandHandler({
  eventStore,
  aggregates
})

const command = {
  aggregateId: '1',
  aggregateName: 'User',
  type: 'create',
  name: 'User Name',
  email: 'test@user.com'
}

execute(command).then(event => {
  console.log('Event saved', event);
})