Neo4j-Swift

The Swift driver ecosystem for Neo4j graph database

Swift 6.0+ | macOS 14+ | iOS 17+ | tvOS 17+ | watchOS 10+ | Linux

Getting Started

Add Theo to your Swift Package Manager dependencies:

.package(url: "https://github.com/Neo4j-Swift/Neo4j-Swift.git", from: "6.0.0")

Then import and use:

import Theo

let client = try BoltClient(
    hostname: "localhost",
    port: 7687,
    username: "neo4j",
    password: "your-password",
    encrypted: true
)

// Connect
try await client.connect()

// Create a node
let node = Node(label: "Person", properties: ["name": "Neo"])
let createdNode = try await client.createAndReturnNode(node: node)

// Run Cypher queries
let result = try await client.executeCypher("MATCH (n) RETURN n LIMIT 10")

Projects

Theo (Neo4j-Swift)

The main Neo4j Swift driver. Provides high-level APIs for CRUD operations on nodes and relationships, transaction support, and Cypher query execution.

  • Full CRUD operations for nodes and relationships
  • Transaction support with rollback capability
  • Cypher query execution with parameterized queries
  • Both synchronous and asynchronous APIs
  • Connection pooling support

Bolt-swift

Swift implementation of the Bolt network protocol. Handles the low-level communication with Neo4j servers including connection management, SSL/TLS encryption, and message serialization.

  • Bolt protocol versions 3, 4.x, and 5.x support
  • SSL/TLS encrypted and unencrypted connections
  • Built on SwiftNIO for high performance
  • Custom certificate validator support
  • Authentication handling for all Bolt versions

PackStream-Swift

PackStream binary message format implementation. Provides serialization and deserialization of data types used in the Bolt protocol.

  • Full PackStream specification support
  • Encoding/decoding for all Neo4j types
  • Bool, Int, Float, String, List, Map, Structure support
  • Efficient binary encoding

Theo-example

Example project demonstrating Theo usage. Includes both a command-line macOS/Linux example and an iOS app with a full UI for connecting to Neo4j and running operations.

  • Command-line example for quick testing
  • iOS app with connection UI
  • Examples for all major operations
  • Best practices demonstration

Code Examples

Creating Nodes

let node = Node(label: "Person", properties: [
    "name": "Thomas Anderson",
    "alias": "Neo"
])

let result = client.createAndReturnNodeSync(node: node)
switch result {
case .failure(let error):
    print("Failed: \(error)")
case .success(let createdNode):
    print("Created node with ID: \(createdNode.id ?? 0)")
}

Creating Relationships

let result = client.relateSync(node: reader, to: writer, type: "follows")
if result.isSuccess {
    print("Relationship created")
}

Running Cypher Queries

let result = client.executeCypherSync(
    "MATCH (n:Person) WHERE n.name = $name RETURN n",
    params: ["name": "Thomas Anderson"]
)

switch result {
case .failure(let error):
    print("Query failed: \(error)")
case .success(let queryResult):
    for row in queryResult.rows {
        print(row)
    }
}

Using Transactions

try client.executeAsTransaction { tx in
    client.executeCypherSync("CREATE (n:Test {prop: 'value1'})")
    client.executeCypherSync("CREATE (n:Test {prop: 'value2'})")

    // Call tx.markAsFailed() to rollback if needed
}

Compatibility

Neo4j Version Bolt Version Status
Neo4j 3.5 Bolt 3 Supported
Neo4j 4.x Bolt 4.0-4.4 Supported
Neo4j 5.x Bolt 5.0-5.6 Supported
Neo4j Aura Bolt 5.x Supported