Swift bindings for FoundationDB, providing a native Swift API for interacting with FoundationDB clusters.
import FoundationDB
// Initialize FoundationDB
try await FDBClient.initialize()
let database = try FDBClient.openDatabase()// Simple key-value operations
try await database.withTransaction { transaction in
// Set a value
let key = "hello"
let value = "world"
transaction.setValue([UInt8](value.utf8), for: [UInt8](key.utf8))
// Get a value
if let valueBytes = try await transaction.getValue(for: [UInt8](key.utf8)) {
print(String(decoding: valueBytes, as: UTF8.self)) // "world"
}
// Delete a key
transaction.clear(key: [UInt8](key.utf8))
}// Efficient streaming over large result sets
let sequence = transaction.getRange(
beginSelector: .firstGreaterOrEqual([UInt8]("user:".utf8)),
endSelector: .firstGreaterOrEqual([UInt8]("user;".utf8))
)
for try await (key, value) in sequence {
let userId = String(decoding: key, as: UTF8.self)
let userData = String(decoding: value, as: UTF8.self)
// Process each key-value pair as it streams
}try await database.withTransaction { transaction in
// Atomic increment
let counterKey = "counter"
let increment = withUnsafeBytes(of: Int64(1).littleEndian) { Array($0) }
transaction.atomicOp(key: [UInt8](counterKey.utf8), param: increment, mutationType: .add)
}- Swift 6.1+
- FoundationDB 7.1+ client library and headers
pkg-config- macOS 12+ / Linux
Whether you are building this repository directly or adding it as a Swift
package dependency, the system needs the FoundationDB C client library and a
pkg-config file so that the Swift Package Manager can locate it. Follow the
steps below once on any machine that will compile the package.
Download and install the FoundationDB client package for your platform from the FoundationDB releases page. This provides:
- The shared library (
libfdb_c.dylibon macOS,libfdb_c.soon Linux) - The C headers at
<your-install-prefix>/include/foundationdb/
brew install pkg-config # macOS
# or: apt install pkg-config / yum install pkgconfigThe Swift Package Manager uses pkg-config to find the library at build
time. A template is included in the repo at
Sources/CFoundationDB/include/CFoundationDB_mac.pc (and ..._linux.pc).
Copy it to a directory on your PKG_CONFIG_PATH. Note that the file
must be named libfdb.pc:
# Common locations — use whichever exists on your system:
# /usr/local/lib/pkgconfig/ (default on macOS/Linux)
# /opt/homebrew/lib/pkgconfig/ (Homebrew)
sudo cp Sources/CFoundationDB/include/CFoundationDB_mac.pc \
/usr/local/lib/pkgconfig/libfdb.pcOpen the copy and set prefix to the root of your FoundationDB installation:
prefix=/usr/local # adjust to your actual install prefix
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: CFoundationDB
Description: The foundationdb C library
Version: 7.3.77 # adjust to match your installed version
Cflags: -I${includedir}
Libs: -L${libdir} -lfdb_cImportant:
Cflagsmust point to the parent of thefoundationdb/directory (i.e.,${includedir}, not${includedir}/foundationdb). The header is included as#include <foundationdb/fdb_c.h>, so the compiler needs to search from the directory that contains thefoundationdb/folder.
Verify the configuration before building:
pkg-config --cflags --libs libfdb
# Expected output (paths will match your prefix):
# -I/usr/local/include -L/usr/local/lib -lfdb_cAdd the package to your Package.swift:
dependencies: [
.package(url: "https://github.com/FoundationDB/fdb-swift-bindings", branch: "main")
]After completing the setup above to make the FoundationDB C client visible, build with:
swift buildTo run the tests, there must be a locally accessible FoundationDB cluster. The standard FoundationDB installer registers a launch daemon (macOS) or systemd service (Linux) that starts an FDB server automatically, with a cluster file at:
- macOS:
/usr/local/etc/foundationdb/fdb.cluster - Linux:
/etc/foundationdb/fdb.cluster
If you are using a custom or minimal install the server may not start automatically. Consult your installation's documentation.
To use a non-default cluster file, set the FDB_CLUSTER_FILE environment variable
when running tests:
FDB_CLUSTER_FILE=path/to/fdb.cluster swift testThe libfdb_c dynamic library must also be on the dynamic library search path
at test run time. This is automatic with a standard install. For a custom
install prefix, add the library directory to the path before running tests by adjusting
your system path or (on Linux) using the LD_LIBRARY_PATH environment variable.
For detailed API documentation and advanced usage patterns, see the inline documentation in the source files.
Licensed under the Apache License, Version 2.0. See LICENSE for details.