Skip to content

Commit 8c7db16

Browse files
committed
Driver/Swift: Add dedicated page
1 parent 1d3251e commit 8c7db16

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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:6.0
23+
24+
import PackageDescription
25+
26+
let package = Package(
27+
name: "CrateDbDemo",
28+
dependencies: [
29+
.package(url: "https://github.com/vapor/postgres-kit.git", "2.0.0"..<"3.0.0")
30+
],
31+
targets: [
32+
.executableTarget(
33+
name: "CrateDbDemo",
34+
dependencies: [.product(name: "PostgresKit", package: "postgres-kit")],
35+
path: "Sources"
36+
),
37+
]
38+
)
39+
```
40+
`Sources/main.swift`
41+
```swift
42+
import PostgresKit
43+
44+
let configuration = try SQLPostgresConfiguration(url: "postgresql://crate:crate@localhost:5432/doc?tlsmode=disable")
45+
let source = PostgresConnectionSource(sqlConfiguration: configuration)
46+
let pool = EventLoopGroupConnectionPool(
47+
source: source,
48+
maxConnectionsPerEventLoop: 2,
49+
on: MultiThreadedEventLoopGroup.singleton
50+
)
51+
defer { pool.shutdown() }
52+
53+
let db = pool.database(logger: .init(label: "test")).sql()
54+
let rows = try await db.raw("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3;").all().get()
55+
56+
struct Record: Codable {
57+
var mountain: String
58+
var region: String
59+
var height: Int
60+
}
61+
62+
for row in rows {
63+
let record = try row.decode(model: Record.self)
64+
print("\(record.mountain): \(record.height)")
65+
}
66+
```
67+
68+
Start CrateDB using Docker or Podman, then compile and invoke the example program.
69+
```shell
70+
docker run --rm --publish=5432:5432 crate -Cdiscovery.type=single-node
71+
```
72+
```shell
73+
swift run
74+
```
75+
76+
:::{rubric} CrateDB Cloud
77+
:::
78+
79+
For connecting to CrateDB Cloud, use the `tlsmode=require` parameter,
80+
and replace username, password, and hostname with values matching
81+
your environment.
82+
```swift
83+
let configuration = try SQLPostgresConfiguration(url: "postgresql://admin:password@testcluster.cratedb.net:5432/doc?tlsmode=require")
84+
```
85+
86+
87+
[postgres-kit]: https://swiftpackageindex.com/vapor/postgres-kit

0 commit comments

Comments
 (0)