# nocms-aggregate-root

> Aggregate root for event sourcing in node

Latest version **1.0.1** (published 2018-10-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install nocms-aggregate-root
pnpm add nocms-aggregate-root
yarn add nocms-aggregate-root
bun add nocms-aggregate-root
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-10-16 |
| First published | 2018-09-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jørgen Landsnes Johansen |
| Maintainers | nocms |
| Keywords | event sourcing, aggregate root |

## Links

- npm: https://www.npmjs.com/package/nocms-aggregate-root
- Repository: https://github.com/miles-no/nocms-aggregate-root
- Homepage: https://github.com/miles-no/nocms-aggregate-root#readme
- Issues: https://github.com/miles-no/nocms-aggregate-root/issues
- npm.io page: https://npm.io/package/nocms-aggregate-root

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2018-10-16
- 1.0.0 — 2018-09-24

## README

# NoCMS Event Sourcing - Aggregate Root

This is an aggregate root used for event sourcing in node.js. The aggregate root can be configured with a custom event store. It save events raised by commands and iterates over them, returning the final result. An optional custom event bus can also be applied.

Packages for Azure SQL store, Azure Service Bus, and RabbitMQ will eventually be available.

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Dependency Status](https://david-dm.org/miles-no/nocms-aggregate-root.svg)](https://david-dm.org/miles-no/nocms-events)
[![devDependencies](https://david-dm.org/miles-no/nocms-aggregate-root/dev-status.svg)](https://david-dm.org/miles-no/nocms-events?type=dev)

## Installation
Install using npm: `npm i nocms-aggregate-root -S`

## Usage

### Example
```
const customerAggregateRoot = require('nocms-aggregate-root');
customerAggregateRoot
  .setStore(store)
  .setCommandHandlers(commandHandlers)
  .setEventHandlers(eventHandlers);

await customerAggregateRoot.addCommand(id, 'createCustomer', { name: 'Jørgen', isCreate: true });
const customer = await customerAggregateRoot.load(id); // { name: 'Jørgen', streamRevision: 1, id: '1', dispatched: false }
```
Visit the `example/` folder for a complete, running example.


### Creating the store
The aggregate root requires a store for saving events in a stream. The store should be provided, using the `setStore` function, which takes an object with the following API:

#### saveEvent(event)
The event object represents an occurence of a certain event in time. The following fields are required:

| Field         | Description                                    |
|===============|================================================|
| aggregateId   | Id of the aggregate root, which is used to group all the events |
| eventType     | Name of the event type, which is used to resolve the event handler |
| streamRevision | Revision number of the event stream, used for chronologically ordering the events |
| payload       | Object for the data of the event.                                                  |


#### getEvents(aggregateId)
Should return an array of all the events of an aggregate identified by aggregateId, ordered by stream revision.

#### markEventDispatched(aggregateId, streamRevision)
When an event bus is used, in order to make sure that an event is published on the bus, the event is initially flagged as undispatched. After a successful `bus.publish` is called (automatically invoked by this package), store.markEventDispatched is also automatically invoked.

This function should set `dispatched = true` on the event identified by aggregateId and streamRevision.

### Creating the bus




## Commit message format and publishing

This repository is published using `semantic-release`, with the default [AngularJS Commit Message Conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit).

---
_Source: https://npm.io/package/nocms-aggregate-root · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
