Skip to content

Commit 53b8479

Browse files
committed
Driver/Swift: Add dedicated page
1 parent 1d3251e commit 53b8479

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
r"https://aiven.io/",
8787
# HTTPSConnectionPool(host='qz.surister.dev', port=443): Read timed out. (read timeout=15)
8888
r"https://qz.surister.dev/",
89+
# 403 Client Error: Forbidden
90+
r"https://swiftpackageindex.com/",
8991
]
9092

9193
linkcheck_anchors_ignore_for_url += [

docs/connect/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
113113

114114
::::
115115

116+
::::{grid-item-card} Swift
117+
:link: connect-swift
118+
:link-type: ref
119+
:link-alt: Connect to CrateDB using Swift
120+
:padding: 3
121+
:text-align: center
122+
:class-card: sd-pt-3
123+
:class-body: sd-fs-1
124+
:class-title: sd-fs-6
125+
{fab}`swift`
126+
::::
127+
116128
:::::
117129

118130

@@ -187,6 +199,7 @@ javascript
187199
php
188200
python
189201
ruby
202+
swift
190203
natural
191204
All drivers <drivers>
192205
```

docs/connect/swift.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
(connect-swift)=
2+
3+
# Swift
4+
5+
:::{include} /_include/links.md
6+
:::
7+
8+
:::{div} sd-text-muted
9+
Connect to CrateDB from Swift applications.
10+
:::
11+
12+
:::{rubric} About
13+
:::
14+
15+
[postgres-kit] is a non-blocking, event-driven Swift client for PostgreSQL.
16+
17+
:::{rubric} Synopsis
18+
:::
19+
20+
`Package.swift`
21+
```swift
22+
// swift-tools-version: 5.7
23+
// The swift-tools-version declares the minimum version of Swift required to build this package.
24+
25+
import PackageDescription
26+
27+
let package = Package(
28+
name: "CrateDbDemo",
29+
dependencies: [
30+
.package(url: "https://github.com/vapor/postgres-kit.git", "2.0.0"..<"3.0.0")
31+
],
32+
targets: [
33+
.executableTarget(
34+
name: "CrateDbDemo",
35+
dependencies: [.product(name: "PostgresKit", package: "postgres-kit")],
36+
path: "Sources"
37+
),
38+
]
39+
)
40+
```
41+
`Sources/main.swift`
42+
```swift
43+
import PostgresKit
44+
45+
let configuration = try SQLPostgresConfiguration(url: "postgresql://crate:crate@localhost:5432/doc?tlsmode=disable")
46+
let source = PostgresConnectionSource(sqlConfiguration: configuration)
47+
let pool = EventLoopGroupConnectionPool(
48+
source: source,
49+
maxConnectionsPerEventLoop: 2,
50+
on: MultiThreadedEventLoopGroup.singleton
51+
)
52+
defer { pool.shutdown() }
53+
54+
let db = pool.database(logger: .init(label: "test")).sql()
55+
let rows = try await db.raw("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3;").all().get()
56+
57+
struct Record: Codable {
58+
var mountain: String
59+
var region: String
60+
var height: Int
61+
}
62+
63+
for row in rows {
64+
let record = try row.decode(model: Record.self)
65+
print("\(record.mountain): \(record.height)")
66+
}
67+
```
68+
69+
Start CrateDB using Docker or Podman, then compile and invoke example program.
70+
```shell
71+
docker run --rm --publish=5432:5432 crate -Cdiscovery.type=single-node
72+
```
73+
```shell
74+
swift run
75+
```
76+
77+
:::{rubric} CrateDB Cloud
78+
:::
79+
80+
For connecting to CrateDB Cloud, use the `tlsmode=require` parameter,
81+
and replace username, password, and hostname with values matching
82+
your environment.
83+
```swift
84+
let configuration = try SQLPostgresConfiguration(url: "postgresql://admin:[email protected]:5432/doc?tlsmode=require")
85+
```
86+
87+
88+
[postgres-kit]: https://swiftpackageindex.com/vapor/postgres-kit

0 commit comments

Comments
 (0)