Skip to content

Latest commit

 

History

History

two-phase

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Two-Phase Transfer Node.js Sample

Code for this sample is in ./main.js.

Prerequisites

Linux >= 5.6 is the only production environment we support. But for ease of development we also support macOS and Windows.

  • NodeJS >= 18

Setup

First, clone this repo and cd into tigerbeetle/src/clients/node/samples/two-phase.

Then, install the TigerBeetle client:

npm install tigerbeetle-node

Start the TigerBeetle server

Follow steps in the repo README to run TigerBeetle.

If you are not running on port localhost:3000, set the environment variable TB_ADDRESS to the full address of the TigerBeetle server you started.

Run this sample

Now you can run this sample:

node main.js

Walkthrough

Here's what this project does.

1. Create accounts

This project starts by creating two accounts (1 and 2).

2. Create pending transfer

Then it begins a pending transfer of 500 of an amount from account 1 to account 2.

3. Fetch and validate pending account balances

Then it fetches both accounts and validates that account 1 has:

  • debits_posted = 0
  • credits_posted = 0
  • debits_pending = 500
  • and credits_pending = 0

And that account 2 has:

  • debits_posted = 0
  • credits_posted = 0
  • debits_pending = 0
  • and credits_pending = 500

(This is because a pending transfer only affects pending credits and debits on accounts, not posted credits and debits.)

4. Post pending transfer

Then it creates a second transfer that marks the first transfer as posted.

5. Fetch and validate transfers

Then it fetches both transfers, validates that the two transfers exist, validates that the first transfer had (and still has) a pending flag, and validates that the second transfer had (and still has) a post_pending_transfer flag.

6. Fetch and validate final account balances

Finally, it fetches both accounts, validates they both exist, and checks that credits and debits for both account are now posted, not pending.

Specifically, that account 1 has:

  • debits_posted = 500
  • credits_posted = 0
  • debits_pending = 0
  • and credits_pending = 0

And that account 2 has:

  • debits_posted = 0
  • credits_posted = 500
  • debits_pending = 0
  • and credits_pending = 0