GithubHelp home page GithubHelp logo

nawawishkid / node-sagas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from slavapanevskiy/node-sagas

1.0 1.0 0.0 162 KB

A convenient tool for managing distributed transaction

License: MIT License

JavaScript 5.32% TypeScript 94.68%

node-sagas's Introduction

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.

Installing / Getting started

This module is installed via npm::

npm i --save node-sagas 

Example

This package provides you with main classes for creating sagas. The first main class is SagaBuilder.

  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:

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.

node-sagas's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.