一个基于 PostgresNIO 和 PostgresKit 的简单连接池。
此软件包需要 Swift 5.7 或更高版本(至少 Xcode 13),并且可以在 macOS(>= macOS 10.15)和 Linux 上编译。
dependencies: [
.package(url: "https://github.com/Outdooractive/PostgresConnectionPool.git", from: "0.7.0"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "PostgresConnectionPool", package: "PostgresConnectionPool"),
]),
]
另请参阅 API 文档。
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()
请创建一个 issue 或打开一个 pull request,以提交修复或增强功能。
MIT
Thomas Rasch, Outdooractive