Skip to content

Commit ae340f9

Browse files
committed
Connect: Add Swift
1 parent c2721f4 commit ae340f9

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
r"https://qz.surister.dev/",
8989
# Read timed out.
9090
r"https://flowfuse.com/",
91+
# 403 Client Error: Forbidden
92+
r"https://swiftpackageindex.com/",
9193
]
9294

9395
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/index
190203
natural
191204
All drivers <drivers>
192205
```

docs/connect/swift/index.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 db.raw("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3;").all().wait()
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+
:::{include} ../_cratedb.md
69+
:::
70+
```shell
71+
swift run
72+
```
73+
74+
:::{rubric} CrateDB Cloud
75+
:::
76+
77+
For connecting to CrateDB Cloud, use the `tlsmode=require` parameter,
78+
and replace username, password, and hostname with values matching
79+
your environment.
80+
```swift
81+
let configuration = try SQLPostgresConfiguration(url: "postgresql://admin:[email protected]:5432/doc?tlsmode=require")
82+
```
83+
84+
85+
[postgres-kit]: https://swiftpackageindex.com/vapor/postgres-kit

0 commit comments

Comments
 (0)