SwiftSwift driver

This section describes Fauna’s open source Swift driver, which provides the resources required to interact with FaunaDB.

Supported platforms

  • iOS 9.0+

  • OSX 10.10+

  • tvOS 9.0+

  • watchOS 2.0+

  • Xcode 9+

  • Swift 4

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

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!