Node.js connector
Install, connect, query and handle errors with the Raft DB Node.js connector (no dependencies, TypeScript types included).
The Node.js connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.
No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.
Requirements#
Node 18 or newer. Plain ES modules, no build step, no dependencies. index.d.ts holds the types.
Install#
Download the npm package from the Download page and install it:
npm install ./raftdb-node-connector-<version>.tgzThe package is an ES module (import { Client } from 'raftdb-node-connector') and carries its TypeScript types (index.d.ts), so no @types package is needed.
Connect#
import { Client, RaftDBError } from 'raftdb-node-connector';
// defaults: host 127.0.0.1, port 7480, timeoutMs 30000; user + password, or token, or nothing
const db = await Client.connect({ host: '127.0.0.1', port: 7480, user: 'admin', password: 's3cret', timeoutMs: 30000 });Query#
await db.put('u1', { name: 'Asha', age: 31 }); // false (new key)
console.log(await db.get('u1')); // { name: 'Asha', age: 31 }
console.log(await db.count({ age: { $gte: 30 } })); // 1
for await (const row of db.scan('SELECT name FROM documents ORDER BY name')) console.log(row);
await db.close();Paging and concurrent calls (from examples.js)#
const page = await db.query({ age: { $gte: 30 } }, { limit: 10 });
console.log('page rows:', page.rows.length, 'next cursor:', page.next); // next is a string, '0' = done
// Several calls at once on one client are queued and run in order.
const [a, b] = await Promise.all([db.get('demo-1'), db.ping()]);Errors#
RaftDBError has .code and .message. A plain Error is a network failure, a timeout or a call after close(). After code 7 the connection is closed.
try { await db.count('{"age":'); }
catch (e) { if (e instanceof RaftDBError) console.log(e.code, e.message); else throw e; }Transactions and prepared statements#
The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.
Notes#
- One request is in flight per connection; calls made meanwhile are queued, so
Promise.allon one client is safe. - Rows are parsed with
JSON.parse: integers above 2^53 lose precision;getRawreturns the exact JSON text. - Cursors are strings (BigInt is accepted on input).
Conformance status#
24/24 conformance scenarios (the test file has two extra tests). The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.