# node-sagas

> Library for handling distributed transactions in the microservices architecture

Latest version **0.0.6** (published 2020-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-sagas
pnpm add node-sagas
yarn add node-sagas
bun add node-sagas
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2020-08-02 |
| First published | 2020-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 129.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 76 |
| Author | Viacheslav Panevskyi |
| Maintainers | slava.panevskyi |
| Keywords | saga, sagas, microservice, microservices, distributed transaction, msa, microservice-architecture |

## Links

- npm: https://www.npmjs.com/package/node-sagas
- Repository: https://github.com/SlavaPanevskiy/node-sagas
- Homepage: https://github.com/SlavaPanevskiy/node-sagas#readme
- Issues: https://github.com/SlavaPanevskiy/node-sagas/issues
- npm.io page: https://npm.io/package/node-sagas

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.0.6 (latest) — 2020-08-02
- 0.0.5 — 2020-08-02
- 0.0.4 — 2020-04-29
- 0.0.3 — 2020-04-29
- 0.0.2 — 2020-04-13
- 0.0.1 — 2020-04-12

## README

# Node sagas 
This is a repository for the `node-sagas` package.
`node-sagas` is a convenient library for managing data consistency in a microservice architecture.
It helps create distributed transaction across services.

Why Sagas?

A distinctive characteristic of the microservice architecture is that in order to ensure loose coupling each service’s 
data is private. Unlike in a monolithic application, you no longer have a single database that any module 
of the application can update. As a result, one of the key challenges that you will face is maintaining
data consistency across services.

Please read more about [saga pattern](https://chrisrichardson.net/post/microservices/2019/07/09/developing-sagas-part-1.html).

## Installing / Getting started

This module is installed via npm::

```shell
npm i --save node-sagas 
```

## Example

This package provides you with main classes for creating sagas.
The first main class is `SagaBuilder`. 
```typescript
  import { SagaBuilder } from 'node-sagas';
  
  const sagaBuilder = new SagaBuilder<CreateOrderSagaParams>();
  const saga = sagaBuilder
    .step('Create order')
    .invoke((params: CreateOrderSagaParams) => {
      // create order logic
    })
    .withCompensation((params: CreateOrderSagaParams) => {
      // reject order logic
    })
    .step('Reserve credit')
    .invoke((params: CreateOrderSagaParams) => {
      // reserve credit
    })
    .step('Approve order')
    .invoke((params: CreateOrderSagaParams) => {
      // approve order
    })
    .build();

    const params = new CreateOrderSagaParams();

    try {
      return await saga.execute(params);
    } catch (e) {
      if (e instanceof SagaExecutionFailed) {
        // Throws, when invocation flow was failed, but compensation has been completed
      }
      if (e instanceof SagaCompensationFailed) {
        // Throws, when compensation flow was failed
      }
    }
```

A step could be defined using `step()` method, for each step you can set an action for a positive 
case with `invoke()` method. Also for each step, you can define compensation action with `withCompensation()` method.

`SagaBuilder` class use generic class for the handler's params:
```typescript
export class CreateOrderSagaParams {
  private orderId: number;
  private customerId: number;

  public getOrderId() {
    return this.orderId;
  }

  public setOrderId(orderId) {
    this.orderId = orderId;
  }

  public getCustomerId() {
    return this.customerId;
  }

  public setCustomerId(customerId) {
    this.customerId = customerId;
  }
}
````
That class represents scope with params between the steps. Also, an instance of that class will be returned 
after the saga success execution.  

## Links

The article on Medium with a practical example will be prepared soon.

## Licensing

The code in this project is licensed under MIT license.

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