Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
r"https://docs.astronomer.io/",
r"https://www.confluent.io/",
r"https://www.tensorflow.org/",
# 403 Client Error: Forbidden
r"https://swiftpackageindex.com/",
Comment on lines +113 to +114

This comment was marked as duplicate.

]

linkcheck_anchors_ignore_for_url += [
Expand Down
12 changes: 12 additions & 0 deletions docs/connect/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ Scala
Visual Basic
::::

::::{grid-item-card}
:link: connect-swift
:link-type: ref
:link-alt: Connect to CrateDB using Swift
:class-body: sd-fs-1 sd-text-center
:class-footer: sd-fs-5 sd-font-weight-bold
{fab}`swift`
+++
Swift
::::

::::{grid-item-card}
:link: connect-zig
:link-type: ref
Expand Down Expand Up @@ -352,6 +363,7 @@ r/index
ruby
rust/index
scala/index
swift/index
odbc/index
visualbasic/index
zig/index
Expand Down
86 changes: 86 additions & 0 deletions docs/connect/swift/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
(connect-swift)=

# Swift

:::{include} /_include/links.md
:::

:::{div} sd-text-muted
Connect to CrateDB from Swift applications.
:::

:::{rubric} About
:::

[postgres-kit] is a non-blocking, event-driven Swift client for PostgreSQL.

:::{rubric} Synopsis
:::

`Package.swift`
```swift
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "CrateDbDemo",
dependencies: [
.package(url: "https://github.com/vapor/postgres-kit.git", "2.0.0"..<"3.0.0")
],
targets: [
.executableTarget(
name: "CrateDbDemo",
dependencies: [.product(name: "PostgresKit", package: "postgres-kit")],
path: "Sources"
),
]
)
```
`Sources/main.swift`
```swift
import PostgresKit

let configuration = try SQLPostgresConfiguration(url: "postgresql://crate:crate@localhost:5432/?tlsmode=disable")
let source = PostgresConnectionSource(sqlConfiguration: configuration)
let pool = EventLoopGroupConnectionPool(
source: source,
maxConnectionsPerEventLoop: 2,
on: MultiThreadedEventLoopGroup.singleton
)
defer { pool.shutdown() }

let db = pool.database(logger: .init(label: "test")).sql()
let rows = try db.raw("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3;").all().wait()

struct Record: Codable {
var mountain: String
var region: String
var height: Int
}

for row in rows {
let record = try row.decode(model: Record.self)
print("\(record.mountain): \(record.height)")
}
```

:::{include} ../_cratedb.md
:::
```shell
swift run
```

:::{rubric} SSL connection
:::

Use the `tlsmode=require` parameter, and replace username, password,
and hostname with values matching your environment.
Also use this variant to connect to CrateDB Cloud.

```swift
let configuration = try SQLPostgresConfiguration(url: "postgresql://admin:password@testcluster.cratedb.net:5432/?tlsmode=require")
```


[postgres-kit]: https://swiftpackageindex.com/vapor/postgres-kit