From 67007c3e69bf2bef76ca545e02683374b38f41fa Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 17 Oct 2025 16:52:28 +0200 Subject: [PATCH] Connect: Add Swift --- docs/conf.py | 2 + docs/connect/index.md | 12 ++++++ docs/connect/swift/index.md | 86 +++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 docs/connect/swift/index.md diff --git a/docs/conf.py b/docs/conf.py index 8ab8f552..56ea5975 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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/", ] linkcheck_anchors_ignore_for_url += [ diff --git a/docs/connect/index.md b/docs/connect/index.md index 84404e82..c4b3fc8d 100644 --- a/docs/connect/index.md +++ b/docs/connect/index.md @@ -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 @@ -352,6 +363,7 @@ r/index ruby rust/index scala/index +swift/index odbc/index visualbasic/index zig/index diff --git a/docs/connect/swift/index.md b/docs/connect/swift/index.md new file mode 100644 index 00000000..a152bd48 --- /dev/null +++ b/docs/connect/swift/index.md @@ -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