Socket
Socket
Sign inDemoInstall

resolve-command

Package Overview
Dependencies
7
Maintainers
3
Versions
736
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    resolve-command

Creates a function that handles commands in a reSolve application.


Version published
Weekly downloads
1.1K
increased by33.99%
Maintainers
3
Install size
460 kB
Created
Weekly downloads
 

Readme

Source

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:

  • aggregates
    An array of aggregates.

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

Keywords

FAQs

Last updated on 26 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc