diff --git a/docs/conf.py b/docs/conf.py index b2cfe734..97aa7970 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -88,6 +88,8 @@ r"https://qz.surister.dev/", # Read timed out. r"https://flowfuse.com/", + # 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 ffe0cae1..3763f502 100644 --- a/docs/connect/index.md +++ b/docs/connect/index.md @@ -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` +:::: + ::::: @@ -187,6 +199,7 @@ javascript php python ruby +swift/index natural All drivers ``` diff --git a/docs/connect/swift/index.md b/docs/connect/swift/index.md new file mode 100644 index 00000000..740345b4 --- /dev/null +++ b/docs/connect/swift/index.md @@ -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:password@testcluster.cratedb.net:5432/doc?tlsmode=require") +``` + + +[postgres-kit]: https://swiftpackageindex.com/vapor/postgres-kit