Skip to content
Open
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 @@ -88,6 +88,8 @@
r"https://qz.surister.dev/",
# Read timed out.
r"https://flowfuse.com/",
# 403 Client Error: Forbidden
r"https://swiftpackageindex.com/",
Comment on lines +91 to +92

This comment was marked as duplicate.

]

linkcheck_anchors_ignore_for_url += [
Expand Down
13 changes: 13 additions & 0 deletions docs/connect/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an

::::

::::{grid-item-card} Swift
:link: connect-swift
:link-type: ref
:link-alt: Connect to CrateDB using Swift
:padding: 3
:text-align: center
:class-card: sd-pt-3
:class-body: sd-fs-1
:class-title: sd-fs-6
{fab}`swift`
::::

:::::


Expand Down Expand Up @@ -187,6 +199,7 @@ javascript
php
python
ruby
swift/index
natural
All drivers <drivers>
```
Expand Down
85 changes: 85 additions & 0 deletions docs/connect/swift/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
(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/doc?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} CrateDB Cloud
:::

For connecting to CrateDB Cloud, use the `tlsmode=require` parameter,
and replace username, password, and hostname with values matching
your environment.
```swift
let configuration = try SQLPostgresConfiguration(url: "postgresql://admin:[email protected]:5432/doc?tlsmode=require")
```


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