Options
All
  • Public
  • Public/Protected
  • All
Menu

SoftwareTransaction is an implementation of software transaction memory, described in greater detail here. The idea is that you can have a block that contains multiple reads and writes, then we'll commit all those changes as a single transaction, automatically retrying as necessary.

Inside the .transact() block, all reads and writes must go through the transaction, not the client, or they won't be tracked.

For example, this is a safe way to move 'money' between bank/account1 and bank/account2:

const amount = 42;

etcd3.stm().transact(tx => {
  return Promise.all([
    tx.get('bank/account1').number(),
    tx.get('bank/account2').number(),
  ]).then(([balance1, balance2]) => {
    if (balance1 < amount) {
      throw new Error('You do not have enough money to transfer!');
    }

    return Promise.all([
      tx.put('bank/account1').value(balance1 - amount),
      tx.put('bank/account2').value(balance2 + amount),
    });
  });
});

(Note: the author does not condone using etcd for your banking infrastructure)

Hierarchy

  • SoftwareTransaction

Index

Constructors

Methods

Constructors

constructor

Methods

delete

get

put

  • .put() starts making a put request against etcd.

    Parameters

    • key: string | Buffer

    Returns PutBuilder

transact

  • transact<T>(fn: (tx: this) => T | PromiseLike<T>): Promise<T>
  • transact runs the function with the current configuration. It will be retried until the transaction succeeds, or until the maximum number of retries has been exceeded.

    Type parameters

    • T

    Parameters

    • fn: (tx: this) => T | PromiseLike<T>
        • (tx: this): T | PromiseLike<T>
        • Parameters

          • tx: this

          Returns T | PromiseLike<T>

    Returns Promise<T>

Legend

  • Constructor
  • Method
  • Inherited method

Generated using TypeDoc