resolve-command
TypeScript icon, indicating that this package has built-in type declarations

0.26.5 • Public • Published

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.
    • jwt - non-verified actual JSON Web 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

Package Sidebar

Install

npm i resolve-command

Weekly Downloads

99

Version

0.26.5

License

MIT

Unpacked Size

57.3 kB

Total Files

10

Last publish

Collaborators

  • resolve-admin
  • reimagined-admin
  • vladihost
  • lykoi18