JSPM

resolve-command

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

Creates a function that handles commands in a reSolve application.

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 eventstore based on definitions of aggregates and their commands.

Usage

When initializing a command, pass the following arguments:

Each aggregate can have optional property snapshotAdapter for managing snapshots mechanism. If property had not been passed, snapshots are turned off by default. Property snapshotAdapter receives the adapter for loading and saving intermediate aggregate state.

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 - a unique aggregate id.
    • aggregateName - the name of aggregate that has to handle the command.
    • type - the command type.
    • jwtToken - non-verified actual JWT token provided from client.

Example

Define a news handling aggregate (see the news-aggregate.js file), use the resolve-command library to execute the createNews command and send the corresponding event to the specified event store. To see a read model handling events which this aggregate produces, refer to the resolve-query package documentation.

import createStorageLiteAdapter from 'resolve-eventstore-lite'
import createCommandExecutor from 'resolve-command'
import newsAggregate from './news-aggregate'
...
  const aggregates = [newsAggregate]
  const memoryStorage = createStorageLiteAdapter({ databaseFile: ':memory:' })

  const executeCommand = createCommandExecutor({
    aggregates
  })

  const event = await executeCommand({
    aggregateId: 'aggregate-id',
    aggregateName: 'news',
    type: 'createNews',
    payload: {
      title: 'News',
      userId: 'user-id',
      text: 'News content'
    }
  })
news-aggregate.js
import Immutable from 'seamless-immutable'

export default {
  name: 'news',
  projection: {
    Init: () => Immutable({}),
    NEWS_CREATED: (state, { payload: { userId } }) =>
      state.merge({
        createdAt: Date.now(),
        createdBy: userId,
        voted: [],
        comments: {}
      })
  },
  commands: {
    createNews: (state, { payload: { title, link, userId, text } }) => {
      if (state.createdAt) {
        throw new Error('Aggregate already exists')
      }

      if (!title) {
        throw new Error('Title is required')
      }

      if (!userId) {
        throw new Error('UserId is required')
      }

      return {
        type: 'NEWS_CREATED',
        payload: {
          title,
          text,
          link,
          userId
        }
      }
    }
  }
}

Analytics