An open API service indexing awesome lists of open source software.

https://github.com/outdooractive/postgresconnectionpool

A simple connection pool on top of PostgresNIO written in Swift.
https://github.com/outdooractive/postgresconnectionpool

pool postgres-kit postgres-nio postgresql swift

Last synced: 5 months ago
JSON representation

A simple connection pool on top of PostgresNIO written in Swift.

Awesome Lists containing this project

README

          

# PostgresConnectionPool

A simple connection pool on top of [PostgresNIO](https://github.com/vapor/postgres-nio) and [PostgresKit](https://github.com/vapor/postgres-kit).

[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FOutdooractive%2FPostgresConnectionPool%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/Outdooractive/PostgresConnectionPool)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FOutdooractive%2FPostgresConnectionPool%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/Outdooractive/PostgresConnectionPool)

## Requirements

This package requires Swift 5.7 or higher (at least Xcode 13), and compiles on macOS (\>= macOS 10.15) and Linux.

## Installation with Swift Package Manager

```swift
dependencies: [
.package(url: "https://github.com/Outdooractive/PostgresConnectionPool.git", from: "0.7.0"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "PostgresConnectionPool", package: "PostgresConnectionPool"),
]),
]
```

## Usage

Please see also the [API documentation](https://swiftpackageindex.com/Outdooractive/PostgresConnectionPool/main/documentation/postgresconnectionpool).

``` swift
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(_ 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()
```

## Contributing

Please create an issue or open a pull request with a fix or enhancement.

## License

MIT

## Author

Thomas Rasch, Outdooractive