A simple connection pool on top of PostgresNIO and PostgresKit.
This package requires Swift 5.7 or higher (at least Xcode 13), and compiles on macOS (>= macOS 10.15) and Linux.
dependencies: [
.package(url: "https://github.com/Outdooractive/PostgresConnectionPool.git", from: "0.7.0"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "PostgresConnectionPool", package: "PostgresConnectionPool"),
]),
]
Please see also the API documentation.
import PostgresConnectionPool
import PostgresKit
import PostgresNIO
var logger = Logger(label: "TestApp")
logger.logLevel = .debug
let postgresConfiguration = PostgresConnection.Configuration(
host: "postgres",
port: 5432,
username: "testuser",
password: "testpassword",
database: "test",
tls: .disable)
let configuration = PoolConfiguration(
applicationName: "TestApp",
postgresConfiguration: postgresConfiguration,
connectTimeout: 10.0,
queryTimeout: 60.0,
poolSize: 5,
maxIdleConnections: 1)
let pool = PostgresConnectionPool(configuration: configuration, logger: logger)
// Fetch a connection from the pool and do something with it...
try await pool.connection { connection in
try await connection.query(PostgresQuery(stringLiteral: "SELECT 1"), logger: logger)
}
// With PostgresKit
func fetchObjects<T: Decodable>(_ sql: SQLQueryString) async throws -> [T] {
try await pool.connection({ connection in
return try await connection.sql().raw(sql).all(decoding: T.self)
})
}
// Open connections, current SQL queries, etc.
await print(pool.info())
// Always call `shutdown()` before releasing a pool
await pool.shutdown()
Please create an issue or open a pull request with a fix or enhancement.
MIT
Thomas Rasch, Outdooractive