Skip to content

Commit d2fd317

Browse files
authored
Minor cleanups (#273)
* Bump Swift min to 5.10, update README and API docs, clean up some doc comments, run swift-format, fix warning about retroactive conformance in 6.2. * Apply PR feedback * Fixup those darn docs
1 parent a4212e1 commit d2fd317

File tree

10 files changed

+260
-140
lines changed

10 files changed

+260
-140
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ jobs:
6868
POSTGRES_HOST_AUTH_METHOD: ${{ matrix.postgres-auth }}
6969
POSTGRES_INITDB_ARGS: --auth-host=${{ matrix.postgres-auth }}
7070
steps:
71+
- name: Install curl
72+
run: apt-get update -y -q && apt-get install -y curl
7173
- name: Check out package
7274
uses: actions/checkout@v5
7375
- name: Run local tests
@@ -133,7 +135,7 @@ jobs:
133135
run: |
134136
brew upgrade || true
135137
export PATH="$(brew --prefix)/opt/postgresql@13/bin:$PATH" PGDATA=/tmp/vapor-postgres-test
136-
(brew unlink postgresql@14 || true) && brew install "postgresql@13" && brew link --force "postgresql@13"
138+
brew install "postgresql@17" && brew link --force "postgresql@17"
137139
initdb --locale=C --auth-host "scram-sha-256" -U "${POSTGRES_USER}" --pwfile=<(echo "${POSTGRES_PASSWORD}")
138140
pg_ctl start --wait
139141
timeout-minutes: 15

Package.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.9
1+
// swift-tools-version:5.10
22
import PackageDescription
33

44
let package = Package(
@@ -13,9 +13,9 @@ let package = Package(
1313
.library(name: "PostgresKit", targets: ["PostgresKit"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.21.1"),
17-
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.3"),
18-
.package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"),
16+
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.27.0"),
17+
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.33.1"),
18+
.package(url: "https://github.com/vapor/async-kit.git", from: "1.21.0"),
1919
],
2020
targets: [
2121
.target(
@@ -40,6 +40,7 @@ let package = Package(
4040

4141
var swiftSettings: [SwiftSetting] { [
4242
.enableUpcomingFeature("ExistentialAny"),
43+
.enableUpcomingFeature("MemberImportVisibility"),
4344
.enableUpcomingFeature("ConciseMagicFile"),
4445
.enableUpcomingFeature("ForwardTrailingClosures"),
4546
.enableUpcomingFeature("DisableOutwardActorInference"),

README.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
<p align="center">
2-
<picture>
3-
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/vapor/postgres-kit/assets/1130717/02ca33b1-d788-4846-8280-7cd89f09c397">
4-
<source media="(prefers-color-scheme: light)" srcset="https://github.com/vapor/postgres-kit/assets/1130717/91de055b-0e4f-42f3-8257-946ac40d731c">
5-
<img src="https://github.com/vapor/postgres-kit/assets/1130717/91de055b-0e4f-42f3-8257-946ac40d731c" height="96" alt="PostgresKit">
6-
</picture>
2+
<img src="https://design.vapor.codes/images/vapor-postgreskit.svg" height="96" alt="PostgresKit">
73
<br>
84
<br>
95
<a href="https://docs.vapor.codes/4.0/"><img src="https://design.vapor.codes/images/readthedocs.svg" alt="Documentation"></a>
106
<a href="https://discord.gg/vapor"><img src="https://design.vapor.codes/images/discordchat.svg" alt="Team Chat"></a>
117
<a href="LICENSE"><img src="https://design.vapor.codes/images/mitlicense.svg" alt="MIT License"></a>
128
<a href="https://github.com/vapor/postgres-kit/actions/workflows/test.yml"><img src="https://img.shields.io/github/actions/workflow/status/vapor/postgres-kit/test.yml?event=push&style=plastic&logo=github&label=tests&logoColor=%23ccc" alt="Continuous Integration"></a>
139
<a href="https://codecov.io/github/vapor/postgres-kit"><img src="https://img.shields.io/codecov/c/github/vapor/postgres-kit?style=plastic&logo=codecov&label=codecov"></a>
14-
<a href="https://swift.org"><img src="https://design.vapor.codes/images/swift59up.svg" alt="Swift 5.9+"></a>
10+
<a href="https://swift.org"><img src="https://design.vapor.codes/images/swift510up.svg" alt="Swift 5.10+"></a>
1511
</p>
1612

1713
<br>
1814

19-
🐘 Non-blocking, event-driven Swift client for PostgreSQL.
15+
PostgresKit is an [SQLKit] driver for PostgreSQL clients.
16+
17+
## Overview
18+
19+
PostgresKit supports building and serializing Postgres-dialect SQL queries using [SQLKit]'s API. PostgresKit uses [PostgresNIO] to connect and communicate with the database server asynchronously. [AsyncKit] is used to provide connection pooling.
20+
21+
> Important: It is strongly recommended that users who leverage PostgresKit directly (e.g. absent the Fluent ORM layer) take advantage of PostgresNIO's [PostgresClient] API for connection management rather than relying upon the legacy AsyncKit API.
2022
2123
### Usage
2224

23-
Use the SPM string to easily include the dependendency in your `Package.swift` file.
25+
Reference this package in your `Package.swift` to include it in your project.
2426

2527
```swift
2628
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0")
@@ -33,42 +35,30 @@ PostgresKit supports the following platforms:
3335
- Ubuntu 20.04+
3436
- macOS 10.15+
3537

36-
## Overview
37-
38-
PostgresKit is an [SQLKit] driver for PostgreSQL clients. It supports building and serializing Postgres-dialect SQL queries. PostgresKit uses [PostgresNIO] to connect and communicate with the database server asynchronously. [AsyncKit](https://github.com/vapor/async-kit) is used to provide connection pooling.
39-
40-
> [!IMPORTANT]
41-
> It is strongly recommended that users who leverage PostgresKit directly (e.g. absent the Fluent ORM layer) take advantage of PostgresNIO's [PostgresClient] API for connection management rather than relying upon the legacy AsyncKit API.
42-
43-
[SQLKit]: https://github.com/vapor/sql-kit
44-
[PostgresNIO]: https://github.com/vapor/postgres-nio
45-
[AsyncKit]: https://github.com/vapor/async-kit
46-
[PostgresClient]: https://api.vapor.codes/postgresnio/documentation/postgresnio/postgresclient
47-
4838
### Configuration
4939

50-
Database connection options and credentials are specified using a `PostgresConfiguration` struct.
40+
Database connection options and credentials are specified using a ``SQLPostgresConfiguration`` struct.
5141

5242
```swift
5343
import PostgresKit
5444

55-
let configuration = PostgresConfiguration(
45+
let configuration = SQLPostgresConfiguration(
5646
hostname: "localhost",
5747
username: "vapor_username",
5848
password: "vapor_password",
5949
database: "vapor_database"
6050
)
6151
```
6252

63-
URL string based configuration is also supported.
53+
URL-based configuration is also supported.
6454

6555
```swift
66-
guard let configuration = PostgresConfiguration(url: "postgres://...") else {
56+
guard let configuration = SQLPostgresConfiguration(url: "postgres://...") else {
6757
...
6858
}
6959
```
7060

71-
To connect via unix-domain sockets, use `unixDomainSocketPath` instead of `hostname` and `port`.
61+
To connect via unix-domain sockets, use ``SQLPostgresConfiguration/init(unixDomainSocketPath:username:password:database:)`` instead of ``SQLPostgresConfiguration/init(hostname:port:username:password:database:tls:)``.
7262

7363
```swift
7464
let configuration = PostgresConfiguration(
@@ -81,22 +71,22 @@ let configuration = PostgresConfiguration(
8171

8272
### Connection Pool
8373

84-
Once you have a `PostgresConfiguration`, you can use it to create a connection source and pool.
74+
Once you have a ``SQLPostgresConfiguration``, you can use it to create a connection source and pool.
8575

8676
```swift
87-
let eventLoopGroup: EventLoopGroup = ...
88-
defer { try! eventLoopGroup.syncShutdown() }
89-
77+
let eventLoopGroup: EventLoopGroup = NIOSingletons.posixEventLoopGroup
9078
let pools = EventLoopGroupConnectionPool(
9179
source: PostgresConnectionSource(configuration: configuration),
9280
on: eventLoopGroup
9381
)
94-
defer { pools.shutdown() }
82+
83+
// When you're done:
84+
try await pools.shutdownAsync()
9585
```
9686

97-
First create a `PostgresConnectionSource` using the configuration struct. This type is responsible for creating new connections to your database server as needed.
87+
First create a ``PostgresConnectionSource`` using the configuration struct. This type is responsible for creating new connections to your database server as needed.
9888

99-
Next, use the connection source to create an `EventLoopGroupConnectionPool`. You will also need to pass an `EventLoopGroup`. For more information on creating an `EventLoopGroup`, visit SwiftNIO's [documentation](https://apple.github.io/swift-nio/docs/current/NIO/index.html). Make sure to shutdown the connection pool before it deinitializes.
89+
Next, use the connection source to create an `EventLoopGroupConnectionPool`. You will also need to pass an `EventLoopGroup`. For more information on creating an `EventLoopGroup`, visit [SwiftNIO's documentation]. Make sure to shutdown the connection pool before it deinitializes.
10090

10191
`EventLoopGroupConnectionPool` is a collection of pools for each event loop. When using `EventLoopGroupConnectionPool` directly, random event loops will be chosen as needed.
10292

@@ -126,7 +116,7 @@ let postgres = pool.database(logger: ...) // PostgresDatabase
126116
let rows = try postgres.simpleQuery("SELECT version();").wait()
127117
```
128118

129-
Visit [PostgresNIO's docs](https://github.com/vapor/postgres-nio) for more information on using `PostgresDatabase`.
119+
Visit [PostgresNIO's docs] for more information on using `PostgresDatabase`.
130120

131121
### SQLDatabase
132122

@@ -137,4 +127,12 @@ let sql = postgres.sql() // SQLDatabase
137127
let planets = try sql.select().column("*").from("planets").all().wait()
138128
```
139129

140-
Visit [SQLKit's docs](https://github.com/vapor/sql-kit) for more information on using `SQLDatabase`.
130+
Visit [SQLKit's docs] for more information on using `SQLDatabase`.
131+
132+
[SQLKit]: https://github.com/vapor/sql-kit
133+
[SQLKit's docs]: https://api.vapor.codes/sqlkit/documentation/sqlkit
134+
[PostgresNIO]: https://github.com/vapor/postgres-nio
135+
[PostgresNIO's docs]: https://api.vapor.codes/postgresnio/documentation/postgresnio
136+
[AsyncKit]: https://github.com/vapor/async-kit
137+
[PostgresClient]: https://api.vapor.codes/postgresnio/documentation/postgresnio/postgresclient
138+
[SwiftNIO's documentation]: https://swiftpackageindex.com/apple/swift-nio/documentation/nio

Sources/PostgresKit/Docs.docc/PostgresKit.md

Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,127 @@
44
@TitleHeading(Package)
55
}
66

7-
PostgresKit is a library providing an SQLKit driver for PostgresNIO.
7+
PostgresKit is an [SQLKit] driver for PostgreSQL clients.
88

99
## Overview
1010

11-
This package provides the "foundational" level of support for using [Fluent] with PostgreSQL by implementing the requirements of an [SQLKit] driver. It is responsible for:
11+
PostgresKit supports building and serializing Postgres-dialect SQL queries using [SQLKit]'s API. PostgresKit uses [PostgresNIO] to connect and communicate with the database server asynchronously. [AsyncKit] is used to provide connection pooling.
1212

13-
- Managing the underlying PostgreSQL library ([PostgresNIO]),
14-
- Providing a two-way bridge between PostgresNIO and SQLKit's generic data and metadata formats, and
15-
- Presenting an interface for establishing, managing, and interacting with database connections via [AsyncKit].
13+
> Important: It is strongly recommended that users who leverage PostgresKit directly (e.g. absent the Fluent ORM layer) take advantage of PostgresNIO's [PostgresClient] API for connection management rather than relying upon the legacy AsyncKit API.
1614
17-
> Important: It is strongly recommended that users who leverage PostgresKit directly (e.g. absent the Fluent ORM layer) take advantage of PostgresNIO's [PostgresClient] API for connection management rather than relying upon the legacy AsyncKit-based support.
15+
### Usage
1816

19-
> Tip: A FluentKit driver for PostgreSQL is provided by the [FluentPostgresDriver] package.
17+
Reference this package in your `Package.swift` to include it in your project.
2018

21-
## Version Support
19+
```swift
20+
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0")
21+
```
2222

23-
This package uses [PostgresNIO] for all underlying database interactions. It is compatible with all versions of PostgreSQL and all platforms supported by that package.
23+
### Supported Platforms
2424

25-
> Caution: There is one exception to the above at the time of this writing: This package requires Swift 5.8 or newer, whereas PostgresNIO continues to support Swift 5.6.
25+
PostgresKit supports the following platforms:
2626

27-
[SQLKit]: https://swiftpackageindex.com/vapor/sql-kit
28-
[PostgresNIO]: https://swiftpackageindex.com/vapor/postgres-nio
29-
[Fluent]: https://swiftpackageindex.com/vapor/fluent-kit
30-
[FluentPostgresDriver]: https://swiftpackageindex.com/vapor/fluent-postgres-driver
31-
[AsyncKit]: https://swiftpackageindex.com/vapor/async-kit
27+
- Ubuntu 20.04+
28+
- macOS 10.15+
29+
30+
### Configuration
31+
32+
Database connection options and credentials are specified using a ``SQLPostgresConfiguration`` struct.
33+
34+
```swift
35+
import PostgresKit
36+
37+
let configuration = SQLPostgresConfiguration(
38+
hostname: "localhost",
39+
username: "vapor_username",
40+
password: "vapor_password",
41+
database: "vapor_database"
42+
)
43+
```
44+
45+
URL-based configuration is also supported.
46+
47+
```swift
48+
guard let configuration = SQLPostgresConfiguration(url: "postgres://...") else {
49+
...
50+
}
51+
```
52+
53+
To connect via unix-domain sockets, use ``SQLPostgresConfiguration/init(unixDomainSocketPath:username:password:database:)`` instead of ``SQLPostgresConfiguration/init(hostname:port:username:password:database:tls:)``.
54+
55+
```swift
56+
let configuration = PostgresConfiguration(
57+
unixDomainSocketPath: "/path/to/socket",
58+
username: "vapor_username",
59+
password: "vapor_password",
60+
database: "vapor_database"
61+
)
62+
```
63+
64+
### Connection Pool
65+
66+
Once you have a ``SQLPostgresConfiguration``, you can use it to create a connection source and pool.
67+
68+
```swift
69+
let eventLoopGroup: EventLoopGroup = NIOSingletons.posixEventLoopGroup
70+
let pools = EventLoopGroupConnectionPool(
71+
source: PostgresConnectionSource(configuration: configuration),
72+
on: eventLoopGroup
73+
)
74+
75+
// When you're done:
76+
try await pools.shutdownAsync()
77+
```
78+
79+
First create a ``PostgresConnectionSource`` using the configuration struct. This type is responsible for creating new connections to your database server as needed.
80+
81+
Next, use the connection source to create an `EventLoopGroupConnectionPool`. You will also need to pass an `EventLoopGroup`. For more information on creating an `EventLoopGroup`, visit [SwiftNIO's documentation]. Make sure to shutdown the connection pool before it deinitializes.
82+
83+
`EventLoopGroupConnectionPool` is a collection of pools for each event loop. When using `EventLoopGroupConnectionPool` directly, random event loops will be chosen as needed.
84+
85+
```swift
86+
pools.withConnection { conn
87+
print(conn) // PostgresConnection on randomly chosen event loop
88+
}
89+
```
90+
91+
To get a pool for a specific event loop, use `pool(for:)`. This returns an `EventLoopConnectionPool`.
92+
93+
```swift
94+
let eventLoop: EventLoop = ...
95+
let pool = pools.pool(for: eventLoop)
96+
97+
pool.withConnection { conn
98+
print(conn) // PostgresConnection on eventLoop
99+
}
100+
```
101+
102+
### PostgresDatabase
103+
104+
Both `EventLoopGroupConnectionPool` and `EventLoopConnectionPool` can be used to create instances of `PostgresDatabase`.
105+
106+
```swift
107+
let postgres = pool.database(logger: ...) // PostgresDatabase
108+
let rows = try postgres.simpleQuery("SELECT version();").wait()
109+
```
110+
111+
Visit [PostgresNIO's docs] for more information on using `PostgresDatabase`.
112+
113+
### SQLDatabase
114+
115+
A `PostgresDatabase` can be used to create an instance of `SQLDatabase`.
116+
117+
```swift
118+
let sql = postgres.sql() // SQLDatabase
119+
let planets = try sql.select().column("*").from("planets").all().wait()
120+
```
121+
122+
Visit [SQLKit's docs] for more information on using `SQLDatabase`.
123+
124+
[SQLKit]: https://github.com/vapor/sql-kit
125+
[SQLKit's docs]: https://api.vapor.codes/sqlkit/documentation/sqlkit
126+
[PostgresNIO]: https://github.com/vapor/postgres-nio
127+
[PostgresNIO's docs]: https://api.vapor.codes/postgresnio/documentation/postgresnio
128+
[AsyncKit]: https://github.com/vapor/async-kit
32129
[PostgresClient]: https://api.vapor.codes/postgresnio/documentation/postgresnio/postgresclient
130+
[SwiftNIO's documentation]: https://swiftpackageindex.com/apple/swift-nio/documentation/nio
Lines changed: 17 additions & 13 deletions
Loading

Sources/PostgresKit/Docs.docc/theme-settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"logo-shape": { "dark": "#000", "light": "#fff" },
1616
"fill": { "dark": "#000", "light": "#fff" }
1717
},
18-
"icons": { "technology": "/postgreskit/images/vapor-postgreskit-logo.svg" }
18+
"icons": { "technology": "/postgreskit/images/PostgresKit/vapor-postgreskit-logo.svg" }
1919
},
2020
"features": {
2121
"quickNavigation": { "enable": true },

0 commit comments

Comments
 (0)