FoundationDB Node.js Bindings
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
-
Error: The FoundationDB C client library could not be loaded. Please ensure FoundationDB is installed and libfdb_c is in your dynamic library path.
cause The native FoundationDB client library (`libfdb_c.so`, `.dylib`, or `.dll`) is not installed on the system, or its location is not included in the operating system's dynamic library search path.fixInstall the FoundationDB client library for your OS and architecture from foundationdb.org/download/. On Linux, ensure `libfdb_c.so` is in a standard path like `/usr/local/lib` or `LD_LIBRARY_PATH` is set. On macOS, check `DYLD_LIBRARY_PATH`. On Windows, ensure `fdb_c.dll` is in a directory listed in your system's `PATH` environment variable. -
Error: API version may be set only once
cause Attempted to call `fdb.setAPIVersion()` more than once in the application's lifecycle, or after other FoundationDB operations have already initialized the API.fixIdentify all calls to `fdb.setAPIVersion()` and consolidate them into a single invocation at the absolute entry point of your application before any other FoundationDB API usage. -
TypeError: fdb.open is not a function
cause Incorrect import statement used in an ESM context. When a CommonJS module is imported with `import fdb from 'module';` in ESM, the default export may not contain all expected properties.fixIn an ESM context, use `import * as fdb from 'foundationdb';` to correctly import all exports from the CommonJS module. If using CommonJS, `const fdb = require('foundationdb');` is correct.
Warnings
- breaking The `foundationdb` Node.js package requires the FoundationDB C client library (`libfdb_c`) to be installed system-wide on the machine where the application runs. This library is not bundled with the npm package and must be downloaded separately from the official FoundationDB website.
- gotcha You MUST call `fdb.setAPIVersion(version)` exactly once, and it must be the very first FoundationDB-related operation in your application. Calling it after `fdb.open()` or attempting to call it multiple times (even with the same version) can lead to errors or unexpected behavior.
- gotcha Windows support for `node-foundationdb` is currently disabled due to a known missing header file (`fdb_c_types.h`) in the FoundationDB Windows MSI installer, which prevents the native bindings from compiling correctly.
- gotcha On macOS, due to binary sandboxing, you may need to explicitly add `export DYLD_LIBRARY_PATH=/usr/local/lib` to your shell profile (`.zshrc` or `.bash_profile`) to help the system locate the `libfdb_c` dynamic library.
- gotcha This library only supports FoundationDB server versions 6.2.0 or later. Using it with older server versions may lead to unexpected errors or incompatible API behavior. Additionally, the `setAPIVersion` argument must be less than or equal to the FoundationDB cluster's actual API version.
Install
-
npm install foundationdb -
yarn add foundationdb -
pnpm add foundationdb
Imports
- fdb
import fdb from 'foundationdb';
const fdb = require('foundationdb'); - fdb.setAPIVersion
const dbRoot = fdb.open(); fdb.setAPIVersion(700); // Incorrect order, will throw an error.
fdb.setAPIVersion(700); // Must be called before fdb.open()
- Database (TypeScript Type)
import type { Database, Transaction, Directory } from 'foundationdb';
Quickstart
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);
});