FoundationDB Node.js Bindings

2.0.1 · active · verified Wed Apr 22

The `foundationdb` package provides Node.js bindings for interacting with the FoundationDB distributed transactional key-value store. It is currently at version 2.0.1 and appears to have an active release cadence, with several recent updates following its 1.0.0 and 2.0.0 major releases. This library offers low-level access to FoundationDB's core features, including ACID transactions, range reads, key selectors, watches, and directory management. A key differentiator is its direct C++ binding approach, requiring users to install the FoundationDB client library separately on their system. It includes robust support for tuple and JSON encoding for keys and values, simplifying data serialization and deserialization within transactions. The library mandates setting the API version prior to opening a database connection, and it is compatible with FoundationDB server versions 6.2.0 and newer.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to FoundationDB, creating a directory, configuring key/value encoding, performing a transactional write, and reading data using the promise-based API.

const fdb = require('foundationdb');
fdb.setAPIVersion(700); // Must be called before database is opened

(async () => {
  const dbRoot = fdb.open(); // or open('/path/to/fdb.cluster')

  // Scope all of your application's data inside the 'myapp' directory in your database
  const db = dbRoot.at(await fdb.directory.createOrOpen(dbRoot, 'myapp'))
    .withKeyEncoding(fdb.encoders.tuple) // automatically encode & decode keys using tuples
    .withValueEncoding(fdb.encoders.json); // and values using JSON

  await db.doTransaction(async tn => {
    console.log('Book 123 is', await tn.get(['books', 123])); // Book 123 is undefined

    tn.set(['books', 123], {
      title: 'Reinventing Organizations',
      author: 'Laloux'
    });
  });
  
  console.log('now book 123 is', await db.get(['books', 123])); // shorthand for db.doTransaction(...)
})().catch(err => {
  console.error('An error occurred:', err);
  process.exit(1);
});

view raw JSON →