FoundationDB

main

1amageek/fdb-swift-bindings

FoundationDB Swift Bindings

Swift bindings for FoundationDB, providing a native Swift API for interacting with FoundationDB clusters.

Quick Start

Initialize the Client

import FoundationDB

// Initialize FoundationDB
try await FDBClient.initialize()
let database = try FDBClient.openDatabase()

Basic Operations

// 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))
}

Range Queries

// 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
}

Atomic Operations

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)
}

Lifecycle Management

FDB resources are managed automatically via ARC and are safe at process exit. For explicit shutdown (tests, graceful server termination):

// 1. Release all database references
database = nil  // triggers fdb_database_destroy via ARC

// 2. Stop the network
FDBClient.shutdown()  // fdb_stop_network + thread join

Important: shutdown() requires all FDBDatabase instances to be released first. A precondition failure occurs if active databases remain.

If shutdown() is not called, the OS reclaims all resources at process exit. This is safe — FoundationDB is transactional and the server handles client disconnects gracefully.

Requirements

  • Swift 6.1+
  • FoundationDB 7.1+
  • macOS 12+ / Linux

Installation

Add the package to your Package.swift:

dependencies: [
    .package(url: "https://github.com/apple/fdb-swift-bindings", from: "1.0.0")
]

Documentation

For detailed API documentation and advanced usage patterns, see the inline documentation in the source files.

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Apr 09 2026 01:05:50 GMT-0900 (Hawaii-Aleutian Daylight Time)