npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saga-transaction

v1.0.2

Published

Saga transaction library

Downloads

10

Readme

Saga Pattern Implementation

This repository contains an implementation of the Saga pattern, a design pattern used to handle long-running transactions and ensure data consistency in distributed systems.

Overview

The Saga class is the main component of this implementation. It allows you to define a sequence of transactions, each with an execute and a compensate function. When you call the execute method, the Saga class will execute the transactions in the defined order, and if any transaction fails, it will automatically execute the compensate functions in reverse order to undo the changes made by the successful transactions.

The TAsyncResult type is a Promise-based result type that represents the outcome of the execute method. It can either be an ok result with a void value if all transactions are executed successfully, or an err result with an Error if any transaction fails.

Usage

Here's an example of how to use the Saga class:

import { Saga, ITransaction } from 'saga-transaction';

// Define your transactions
const depositTransaction: ITransaction<number> = {
  name: 'Deposit',
  execute: (amount: number) => {
    // Execute the deposit operation
    return Promise.resolve(amount);
  },
  compensate: (amount: number) => {
    // Compensate the deposit operation
    return Promise.resolve(amount);
  },
};

const withdrawTransaction: ITransaction<number> = {
  name: 'Withdraw',
  execute: (amount: number) => {
    // Execute the withdraw operation
    return Promise.resolve(amount);
  },
  compensate: (amount: number) => {
    // Compensate the withdraw operation
    return Promise.resolve(amount);
  },
};

// Create a new Saga and add the transactions
const saga = Saga.new<number>()
  .addTransaction(depositTransaction)
  .addTransaction(withdrawTransaction);

// Execute the Saga
saga.execute(100).then((result) => {
  if (result.isOk()) {
    console.log('Saga executed successfully');
  } else {
    console.error('Saga failed:', result.error);
  }
});

API

Saga class

  • static new<T>(): Saga<T>: Creates a new Saga instance for the given generic type T.
  • addTransaction(transaction: ITransaction<T>): Saga<T>: Adds a transaction to the saga. Returns the Saga instance to allow method chaining.
  • execute(args: T): TAsyncResult<void, Error>: Executes the transactions in the saga. Returns a TAsyncResult that resolves to an ok result with void value if all transactions are executed successfully, or an err result with an Error if any transaction fails.

ITransaction interface

  • name: string: The name of the transaction.
  • execute: (args: T) => Promise<T>: The function that executes the transaction logic.
  • compensate: (args: T) => Promise<T>: The function that compensates the transaction logic.

TAsyncResult type

  • Promise<Result<T, E>>: A Promise-based result type that represents the outcome of an asynchronous operation. It can either be an ok result with a value of type T, or an err result with an error of type E.

Contributing

If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.