Using Neo4j from Swift

Goals
If you are an iOS, macOS or Linux developer using Swift, this guide provides instructions for how to use the Theo framework to interact with Neo4j.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have created an Neo4j AuraDB cloud instance, or installed Neo4j locally

Intermediate

Neo4j Community Drivers

Members of the each programming language community have invested a lot of time and love to develop each one of the community drivers for Neo4j, so if you use any one of them, please provide feedback to the authors.

The community drivers have been graciously contributed by the Neo4j community. Many of them are fully featured and well-maintained, but some may not be. Neo4j does not take any responsibility for their usability.

Neo4j for Swift Developers

swift logo

Swift is a great language for writing iOS and macOS apps, and with the introduction of Swift 3.0, it has become a great language for writing server side applications as well. All of which are excellent places to use Neo4j.

Theo

Theo, the Swift framework for interfacing Neo4j, is now available in it’s version 3.0.

With this version, Theo gets full support for Swift 3, Linux support, and better integration with popular package managers. This on top of features you already expect, like CRUD operations for nodes and relationships, cypher support and transaction support.

Apart from reading the detailed instructions, you can take a look at our example project for how to use Theo in your iOS project.

Theo is available for iOS, macOS and Linux, using package managers such as the Swift Package Manager, CocoaPods and Carthage, as well as a git submodule, regardless if your app is written in Objective-C or Swift.

You can also easily integrate it in server side swift project on macOS or Linux as a git submodule or the Swift Package Manager. If you are new to server side swift development, we recommend you try Theo out together with Vapor. Other suggested web frameworks you can integrate Theo with are Kitura and Perfect.

Theo has been available since Swift 1.2, and has followed the Swift versions since then. For the future we also plan to stay on top of the latest Swift versions. Theo interfaces Neo4j via the REST protocol, support for Bolt is absolutely a priority on our roadmap ahead.

We look forward to hearing what you create using Theo and Neo4j. Feel free to ask any developer questions on the Neo4j Online Community.

Source

https://github.com/GraphStory/neo4j-ios

Docs

https://github.com/GraphStory/neo4j-ios#usage

Example

https://github.com/niklassaers/theo-example

Authors

Cory Wiles, Niklas Saers

Neo4j Online Community

Blog Post

https://neo4j.com/blog/theo-3-0-swift-framework-neo4j/

Getting started with Theo

After installing Theo in your project, you are ready to

import Theo

Then you can instantiate your client like so

let theo: Client = Client(baseURL: "hostname.com", user: "username", pass: "password")

create a node

let node = Node()
let randomString: String = UUID().uuidString

node.setProp("propertyKey_1", propertyValue: "propertyValue_1" + randomString)
node.setProp("propertyKey_2", propertyValue: "propertyValue_2" + randomString)

theo.createNode(node, completionBlock: { (node, error) in
    print("new node \(node)")
})

fetch a node

theo.fetchNode("IDToFetch", completionBlock: { (node, error) in
    print("meta in success \(node!.meta) node \(node) error \(error)")
})

do a cypher query

let cypherQuery: String = "MATCH (node1:Label1)-->(node2:Label2) WHERE node1.propertyA = {value} RETURN node2.propertyA, node2.propertyB"
let cypherParams: Dictionary<String, AnyObject> = ["value" : "someValue"]

theo.executeCypher(cypherQuery, params: cypherParams, completionBlock: { (cypher, error) in
    println("response from cypher \(cypher)")
})

or a transaction

let createStatement: String = "CREATE ( bike:Bike { weight: 10 } ) CREATE ( frontWheel:Wheel { spokes: 3 } ) CREATE ( backWheel:Wheel { spokes: 32 } ) CREATE p1 = bike -[:HAS { position: 1 } ]-> frontWheel CREATE p2 = bike -[:HAS { position: 2 } ]-> backWheel RETURN bike, p1, p2"
let resultDataContents: Array<String> = ["row", "graph"]
let statement: Dictionary <String, AnyObject> = ["statement" : createStatement, "resultDataContents" : resultDataContents]
let statements: Array<Dictionary <String, AnyObject>> = [statement]

theo.executeTransaction(statements, completionBlock: {(response, error) in
    print("response \(response) and error \(error?.description")
})

As you can see, it is very much how you would expect Swift code to read, and it integrates with Neo4j very much how you would expect a Neo4j integration to be. So no hard learning curves, meaning you can start being productive right away.

Happy coding!