Files
Alexander Bayandin 291b4f0d41 Update client libs for test_runner/pg_clients to their latest versions (#4092)
Also, use Workaround D for `swift/PostgresClientKitExample`, 
which supports neither SNI nor connections options
2023-05-04 18:22:04 +01:00

46 lines
1.2 KiB
Swift

import Foundation
import PostgresNIO
import NIOPosix
import Logging
await Task {
do {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let logger = Logger(label: "postgres-logger")
let env = ProcessInfo.processInfo.environment
let sslContext = try! NIOSSLContext(configuration: .makeClientConfiguration())
let config = PostgresConnection.Configuration(
host: env["NEON_HOST"] ?? "",
port: 5432,
username: env["NEON_USER"] ?? "",
password: env["NEON_PASSWORD"] ?? "",
database: env["NEON_DATABASE"] ?? "",
tls: .require(sslContext)
)
let connection = try await PostgresConnection.connect(
on: eventLoopGroup.next(),
configuration: config,
id: 1,
logger: logger
)
let rows = try await connection.query("SELECT 1 as col", logger: logger)
for try await (n) in rows.decode((Int).self, context: .default) {
print(n)
}
// Close your connection once done
try await connection.close()
// Shutdown the EventLoopGroup, once all connections are closed.
try await eventLoopGroup.shutdownGracefully()
} catch {
print(error)
}
}.value