tigerbeetle-node
Advanced tools
Comparing version 0.16.8 to 0.16.9
@@ -1182,2 +1182,18 @@ "use strict"; | ||
}); | ||
test('accept zero-length create_accounts', async () => { | ||
const errors = await client.createAccounts([]); | ||
assert_1.default.deepStrictEqual(errors, []); | ||
}); | ||
test('accept zero-length create_transfers', async () => { | ||
const errors = await client.createTransfers([]); | ||
assert_1.default.deepStrictEqual(errors, []); | ||
}); | ||
test('accept zero-length lookup_accounts', async () => { | ||
const accounts = await client.lookupAccounts([]); | ||
assert_1.default.deepStrictEqual(accounts, []); | ||
}); | ||
test('accept zero-length lookup_transfers', async () => { | ||
const transfers = await client.lookupTransfers([]); | ||
assert_1.default.deepStrictEqual(transfers, []); | ||
}); | ||
async function main() { | ||
@@ -1184,0 +1200,0 @@ const start = new Date().getTime(); |
{ | ||
"name": "tigerbeetle-node", | ||
"version": "0.16.8", | ||
"version": "0.16.9", | ||
"description": "TigerBeetle Node.js client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
268
README.md
@@ -29,2 +29,3 @@ --- | ||
```javascript | ||
const { id } = require("tigerbeetle-node"); | ||
const { createClient } = require("tigerbeetle-node"); | ||
@@ -98,4 +99,4 @@ | ||
```javascript | ||
let account = { | ||
id: 137n, | ||
const account = { | ||
id: id(), // TigerBeetle time-based ID. | ||
debits_pending: 0n, | ||
@@ -115,5 +116,9 @@ debits_posted: 0n, | ||
let accountErrors = await client.createAccounts([account]); | ||
const account_errors = await client.createAccounts([account]); | ||
// Error handling omitted. | ||
``` | ||
See details for the recommended ID scheme in | ||
[time-based identifiers](https://docs.tigerbeetle.com/coding/data-modeling#tigerbeetle-time-based-identifiers-recommended). | ||
### Account Flags | ||
@@ -139,3 +144,3 @@ | ||
```javascript | ||
let account0 = { | ||
const account0 = { | ||
id: 100n, | ||
@@ -153,5 +158,5 @@ debits_pending: 0n, | ||
timestamp: 0n, | ||
flags: 0, | ||
flags: AccountFlags.linked | AccountFlags.debits_must_not_exceed_credits, | ||
}; | ||
let account1 = { | ||
const account1 = { | ||
id: 101n, | ||
@@ -169,7 +174,7 @@ debits_pending: 0n, | ||
timestamp: 0n, | ||
flags: 0, | ||
flags: AccountFlags.history, | ||
}; | ||
account0.flags = AccountFlags.linked | | ||
AccountFlags.debits_must_not_exceed_credits; | ||
accountErrors = await client.createAccounts([account0, account1]); | ||
const account_errors = await client.createAccounts([account0, account1]); | ||
// Error handling omitted. | ||
``` | ||
@@ -190,3 +195,3 @@ | ||
```javascript | ||
let account2 = { | ||
const account0 = { | ||
id: 102n, | ||
@@ -206,3 +211,3 @@ debits_pending: 0n, | ||
}; | ||
let account3 = { | ||
const account1 = { | ||
id: 103n, | ||
@@ -222,3 +227,3 @@ debits_pending: 0n, | ||
}; | ||
let account4 = { | ||
const account2 = { | ||
id: 104n, | ||
@@ -238,4 +243,5 @@ debits_pending: 0n, | ||
}; | ||
accountErrors = await client.createAccounts([account2, account3, account4]); | ||
for (const error of accountErrors) { | ||
const account_errors = await client.createAccounts([account0, account1, account2]); | ||
for (const error of account_errors) { | ||
switch (error.result) { | ||
@@ -272,21 +278,3 @@ case CreateAccountError.exists: | ||
```javascript | ||
const accounts = await client.lookupAccounts([137n, 138n]); | ||
console.log(accounts); | ||
/* | ||
* [{ | ||
* id: 137n, | ||
* debits_pending: 0n, | ||
* debits_posted: 0n, | ||
* credits_pending: 0n, | ||
* credits_posted: 0n, | ||
* user_data_128: 0n, | ||
* user_data_64: 0n, | ||
* user_data_32: 0, | ||
* reserved: 0, | ||
* ledger: 1, | ||
* code: 718, | ||
* flags: 0, | ||
* timestamp: 1623062009212508993n, | ||
* }] | ||
*/ | ||
const accounts = await client.lookupAccounts([100n, 101n]); | ||
``` | ||
@@ -302,4 +290,4 @@ | ||
```javascript | ||
let transfers = [{ | ||
id: 1n, | ||
const transfers = [{ | ||
id: id(), // TigerBeetle time-based ID. | ||
debit_account_id: 102n, | ||
@@ -318,5 +306,10 @@ credit_account_id: 103n, | ||
}]; | ||
let transferErrors = await client.createTransfers(transfers); | ||
const transfer_errors = await client.createTransfers(transfers); | ||
// Error handling omitted. | ||
``` | ||
See details for the recommended ID scheme in | ||
[time-based identifiers](https://docs.tigerbeetle.com/coding/data-modeling#tigerbeetle-time-based-identifiers-recommended). | ||
### Response and Errors | ||
@@ -334,3 +327,50 @@ | ||
```javascript | ||
for (const error of transferErrors) { | ||
const transfers = [{ | ||
id: 1n, | ||
debit_account_id: 102n, | ||
credit_account_id: 103n, | ||
amount: 10n, | ||
pending_id: 0n, | ||
user_data_128: 0n, | ||
user_data_64: 0n, | ||
user_data_32: 0, | ||
timeout: 0, | ||
ledger: 1, | ||
code: 720, | ||
flags: 0, | ||
timestamp: 0n, | ||
}, | ||
{ | ||
id: 2n, | ||
debit_account_id: 102n, | ||
credit_account_id: 103n, | ||
amount: 10n, | ||
pending_id: 0n, | ||
user_data_128: 0n, | ||
user_data_64: 0n, | ||
user_data_32: 0, | ||
timeout: 0, | ||
ledger: 1, | ||
code: 720, | ||
flags: 0, | ||
timestamp: 0n, | ||
}, | ||
{ | ||
id: 3n, | ||
debit_account_id: 102n, | ||
credit_account_id: 103n, | ||
amount: 10n, | ||
pending_id: 0n, | ||
user_data_128: 0n, | ||
user_data_64: 0n, | ||
user_data_32: 0, | ||
timeout: 0, | ||
ledger: 1, | ||
code: 720, | ||
flags: 0, | ||
timestamp: 0n, | ||
}]; | ||
const transfer_errors = await client.createTransfers(batch); | ||
for (const error of transfer_errors) { | ||
switch (error.result) { | ||
@@ -363,4 +403,5 @@ case CreateTransferError.exists: | ||
```javascript | ||
for (let i = 0; i < transfers.len; i++) { | ||
const transferErrors = await client.createTransfers(transfers[i]); | ||
const batch = []; // Array of transfer to create. | ||
for (let i = 0; i < batch.len; i++) { | ||
const transfer_errors = await client.createTransfers(batch[i]); | ||
// Error handling omitted. | ||
@@ -377,6 +418,7 @@ } | ||
```javascript | ||
const batch = []; // Array of transfer to create. | ||
const BATCH_SIZE = 8190; | ||
for (let i = 0; i < transfers.length; i += BATCH_SIZE) { | ||
const transferErrors = await client.createTransfers( | ||
transfers.slice(i, Math.min(transfers.length, BATCH_SIZE)), | ||
for (let i = 0; i < batch.length; i += BATCH_SIZE) { | ||
const transfer_errors = await client.createTransfers( | ||
batch.slice(i, Math.min(batch.length, BATCH_SIZE)), | ||
); | ||
@@ -413,4 +455,4 @@ // Error handling omitted. | ||
```javascript | ||
let transfer0 = { | ||
id: 2n, | ||
const transfer0 = { | ||
id: 4n, | ||
debit_account_id: 102n, | ||
@@ -426,7 +468,7 @@ credit_account_id: 103n, | ||
code: 720, | ||
flags: 0, | ||
flags: TransferFlags.linked, | ||
timestamp: 0n, | ||
}; | ||
let transfer1 = { | ||
id: 3n, | ||
const transfer1 = { | ||
id: 5n, | ||
debit_account_id: 102n, | ||
@@ -445,5 +487,6 @@ credit_account_id: 103n, | ||
}; | ||
transfer0.flags = TransferFlags.linked; | ||
// Create the transfer | ||
transferErrors = await client.createTransfers([transfer0, transfer1]); | ||
const transfer_errors = await client.createTransfers([transfer0, transfer1]); | ||
// Error handling omitted. | ||
``` | ||
@@ -468,4 +511,4 @@ | ||
```javascript | ||
let transfer2 = { | ||
id: 4n, | ||
const transfer0 = { | ||
id: 6n, | ||
debit_account_id: 102n, | ||
@@ -484,6 +527,8 @@ credit_account_id: 103n, | ||
}; | ||
transferErrors = await client.createTransfers([transfer2]); | ||
let transfer3 = { | ||
id: 5n, | ||
let transfer_errors = await client.createTransfers([transfer0]); | ||
// Error handling omitted. | ||
const transfer1 = { | ||
id: 7n, | ||
debit_account_id: 102n, | ||
@@ -493,3 +538,3 @@ credit_account_id: 103n, | ||
amount: amount_max, | ||
pending_id: 4n, | ||
pending_id: 6n, | ||
user_data_128: 0n, | ||
@@ -504,3 +549,5 @@ user_data_64: 0n, | ||
}; | ||
transferErrors = await client.createTransfers([transfer3]); | ||
transfer_errors = await client.createTransfers([transfer1]); | ||
// Error handling omitted. | ||
``` | ||
@@ -517,4 +564,4 @@ | ||
```javascript | ||
let transfer4 = { | ||
id: 4n, | ||
const transfer0 = { | ||
id: 8n, | ||
debit_account_id: 102n, | ||
@@ -533,10 +580,12 @@ credit_account_id: 103n, | ||
}; | ||
transferErrors = await client.createTransfers([transfer4]); | ||
let transfer5 = { | ||
id: 7n, | ||
let transfer_errors = await client.createTransfers([transfer0]); | ||
// Error handling omitted. | ||
const transfer1 = { | ||
id: 9n, | ||
debit_account_id: 102n, | ||
credit_account_id: 103n, | ||
amount: 10n, | ||
pending_id: 6n, | ||
pending_id: 8n, | ||
user_data_128: 0n, | ||
@@ -551,3 +600,5 @@ user_data_64: 0n, | ||
}; | ||
transferErrors = await client.createTransfers([transfer5]); | ||
transfer_errors = await client.createTransfers([transfer1]); | ||
// Error handling omitted. | ||
``` | ||
@@ -570,21 +621,3 @@ | ||
```javascript | ||
transfers = await client.lookupTransfers([1n, 2n]); | ||
console.log(transfers); | ||
/* | ||
* [{ | ||
* id: 1n, | ||
* debit_account_id: 102n, | ||
* credit_account_id: 103n, | ||
* amount: 10n, | ||
* pending_id: 0n, | ||
* user_data_128: 0n, | ||
* user_data_64: 0n, | ||
* user_data_32: 0, | ||
* timeout: 0, | ||
* ledger: 1, | ||
* code: 720, | ||
* flags: 0, | ||
* timestamp: 1623062009212508993n, | ||
* }] | ||
*/ | ||
const transfers = await client.lookupTransfers([1n, 2n]); | ||
``` | ||
@@ -604,3 +637,3 @@ | ||
```javascript | ||
let filter = { | ||
const filter = { | ||
account_id: 2n, | ||
@@ -638,3 +671,3 @@ user_data_128: 0n, // No filter by UserData. | ||
```javascript | ||
filter = { | ||
const filter = { | ||
account_id: 2n, | ||
@@ -667,3 +700,3 @@ user_data_128: 0n, // No filter by UserData. | ||
```javascript | ||
var query_filter = { | ||
const query_filter = { | ||
user_data_128: 1000n, // Filter by UserData. | ||
@@ -679,2 +712,3 @@ user_data_64: 100n, | ||
}; | ||
const query_accounts = await client.queryAccounts(query_filter); | ||
@@ -694,3 +728,3 @@ ``` | ||
```javascript | ||
query_filter = { | ||
const query_filter = { | ||
user_data_128: 1000n, // Filter by UserData. | ||
@@ -706,2 +740,3 @@ user_data_64: 100n, | ||
}; | ||
const query_transfers = await client.queryTransfers(query_filter); | ||
@@ -729,3 +764,3 @@ ``` | ||
```javascript | ||
const batch = []; | ||
const batch = []; // Array of transfer to create. | ||
let linkedFlag = 0; | ||
@@ -755,16 +790,4 @@ linkedFlag |= TransferFlags.linked; | ||
const errors = await client.createTransfers(batch); | ||
/** | ||
* console.log(errors); | ||
* [ | ||
* { index: 1, error: 1 }, // linked_event_failed | ||
* { index: 2, error: 1 }, // linked_event_failed | ||
* { index: 3, error: 25 }, // exists | ||
* { index: 4, error: 1 }, // linked_event_failed | ||
* | ||
* { index: 6, error: 17 }, // exists_with_different_flags | ||
* { index: 7, error: 1 }, // linked_event_failed | ||
* ] | ||
*/ | ||
const transfer_errors = await client.createTransfers(batch); | ||
// Error handling omitted. | ||
``` | ||
@@ -786,9 +809,15 @@ | ||
```javascript | ||
// External source of time. | ||
let historical_timestamp = 0n | ||
// Events loaded from an external source. | ||
const historical_accounts = []; // Loaded from an external source. | ||
const historical_transfers = []; // Loaded from an external source. | ||
// First, load and import all accounts with their timestamps from the historical source. | ||
const accountsBatch = []; | ||
for (let index = 0; i < historicalAccounts.length; i++) { | ||
let account = historicalAccounts[i]; | ||
const accounts = []; | ||
for (let index = 0; i < historical_accounts.length; i++) { | ||
let account = historical_accounts[i]; | ||
// Set a unique and strictly increasing timestamp. | ||
historicalTimestamp += 1; | ||
account.timestamp = historicalTimestamp; | ||
historical_timestamp += 1; | ||
account.timestamp = historical_timestamp; | ||
// Set the account as `imported`. | ||
@@ -798,18 +827,19 @@ account.flags = AccountFlags.imported; | ||
// must be `linked`. | ||
if (index < historicalAccounts.length - 1) { | ||
if (index < historical_accounts.length - 1) { | ||
account.flags |= AccountFlags.linked; | ||
} | ||
accountsBatch.push(account); | ||
accounts.push(account); | ||
} | ||
accountErrors = await client.createAccounts(accountsBatch); | ||
const account_errors = await client.createAccounts(accounts); | ||
// Error handling omitted. | ||
// Then, load and import all transfers with their timestamps from the historical source. | ||
const transfersBatch = []; | ||
for (let index = 0; i < historicalTransfers.length; i++) { | ||
let transfer = historicalTransfers[i]; | ||
const transfers = []; | ||
for (let index = 0; i < historical_transfers.length; i++) { | ||
let transfer = historical_transfers[i]; | ||
// Set a unique and strictly increasing timestamp. | ||
historicalTimestamp += 1; | ||
transfer.timestamp = historicalTimestamp; | ||
historical_timestamp += 1; | ||
transfer.timestamp = historical_timestamp; | ||
// Set the account as `imported`. | ||
@@ -819,12 +849,14 @@ transfer.flags = TransferFlags.imported; | ||
// must be `linked`. | ||
if (index < historicalTransfers.length - 1) { | ||
if (index < historical_transfers.length - 1) { | ||
transfer.flags |= TransferFlags.linked; | ||
} | ||
transfersBatch.push(transfer); | ||
transfers.push(transfer); | ||
} | ||
transferErrors = await client.createAccounts(transfersBatch); | ||
const transfer_errors = await client.createTransfers(transfers); | ||
// Error handling omitted. | ||
// Since it is a linked chain, in case of any error the entire batch is rolled back and can be retried | ||
// with the same historical timestamps without regressing the cluster timestamp. | ||
``` |
@@ -1321,3 +1321,2 @@ import assert, { AssertionError } from 'assert' | ||
test('can import accounts and transfers', async (): Promise<void> => { | ||
@@ -1412,2 +1411,22 @@ const accountTmp: Account = { | ||
test('accept zero-length create_accounts', async (): Promise<void> => { | ||
const errors = await client.createAccounts([]) | ||
assert.deepStrictEqual(errors, []) | ||
}) | ||
test('accept zero-length create_transfers', async (): Promise<void> => { | ||
const errors = await client.createTransfers([]) | ||
assert.deepStrictEqual(errors, []) | ||
}) | ||
test('accept zero-length lookup_accounts', async (): Promise<void> => { | ||
const accounts = await client.lookupAccounts([]) | ||
assert.deepStrictEqual(accounts, []) | ||
}) | ||
test('accept zero-length lookup_transfers', async (): Promise<void> => { | ||
const transfers = await client.lookupTransfers([]) | ||
assert.deepStrictEqual(transfers, []) | ||
}) | ||
async function main () { | ||
@@ -1414,0 +1433,0 @@ const start = new Date().getTime() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
9899208
4246
825