Swift driver
This section describes Fauna’s open source Swift driver, which provides the resources required to interact with FaunaDB.
Install
CocoaPods:
pod 'FaunaDB', '~> 2.0.0'
Carthage:
github 'fauna/faunadb-swift'
SwiftPM:
.Package(url: "https://github.com/fauna/faunadb-swift.git", Version(2, 0, 0))
Usage
import FaunaDB
struct Post {
let title: String
let body: String?
}
extension Post: FaunaDB.Encodable {
func encode() -> Expr {
return Obj(
"title" => title,
"body" => body
)
}
}
extension Post: FaunaDB.Decodable {
init?(value: Value) throws {
try self.init(
title: value.get("title") ?? "Untitled",
body: value.get("body")
)
}
}
let client = FaunaDB.Client(secret: "your-key-secret-here")
// Creating a new post
try! client.query(
Create(
at: Class("posts")
Obj("data" => Post("My swift app", nil))
)
).await(timeout: .now() + 5)
// Retrieve a saved post
let getPost = client.query(Get(Ref(class: Class("posts"), id: "42")))
let post: Post = try! getPost.map { dbEntry in dbEntry.get("data") }
.await(timeout: .now() + 5)
Next steps
-
Driver repository: https://github.com/fauna/faunadb-swift
-
Driver-specific reference documentation.
-
Check out our example Swift project.
-
For more information about FaunaDB query language, consult our query language reference documentation.
Was this article helpful?
We're sorry to hear that.
Tell us how we can improve!
documentation@fauna.com
Thank you for your feedback!