Created
July 18, 2020 19:29
-
-
Save niklassaers/ece353ee80add93ab63f9cd110f0b4b2 to your computer and use it in GitHub Desktop.
Vapor with Theo - Getting started
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
import Theo | |
func theoClient(completion: @escaping (BoltClient) -> ()) { | |
do { | |
let client = try BoltClient(hostname: "127.0.0.1", | |
port: 7687, | |
username: "neo4j", | |
password: "test", | |
encrypted: true) | |
client.connect { result in | |
let isSuccess = try! result.get() | |
assert(isSuccess) | |
completion(client) | |
} | |
} catch { | |
print("Error! \(String(describing: error))") | |
} | |
} | |
func routes(_ app: Application) throws { | |
app.get { req -> EventLoopFuture<String> in | |
let promise: EventLoopPromise<String> = req.eventLoop.makePromise() | |
theoClient() { theo in | |
let newNode = Node(label: "New", properties: [ "createdTime": Date().timeIntervalSince1970 ]) | |
theo.createAndReturnNode(node: newNode) { result in | |
do { | |
let node = try result.get() | |
assert(node.labels.first! == "New") | |
promise.completeWith(.success("Newly created node!")) | |
} catch { | |
promise.completeWith(.failure(error)) | |
} | |
} | |
} | |
return promise.futureResult | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment