|
4 | 4 | @TitleHeading(Package)
|
5 | 5 | }
|
6 | 6 |
|
7 |
| -PostgresKit is a library providing an SQLKit driver for PostgresNIO. |
| 7 | +🐘 Non-blocking, event-driven Swift client for PostgreSQL. |
| 8 | + |
| 9 | +### Usage |
| 10 | + |
| 11 | +Use the SPM string to easily include the dependendency in your `Package.swift` file. |
| 12 | + |
| 13 | +```swift |
| 14 | +.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0") |
| 15 | +``` |
| 16 | + |
| 17 | +### Supported Platforms |
| 18 | + |
| 19 | +PostgresKit supports the following platforms: |
| 20 | + |
| 21 | +- Ubuntu 20.04+ |
| 22 | +- macOS 10.15+ |
8 | 23 |
|
9 | 24 | ## Overview
|
10 | 25 |
|
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: |
| 26 | +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] is used to provide connection pooling. |
| 27 | + |
| 28 | +> 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. |
| 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 = ... |
| 70 | +defer { try! eventLoopGroup.syncShutdown() } |
| 71 | + |
| 72 | +let pools = EventLoopGroupConnectionPool( |
| 73 | + source: PostgresConnectionSource(configuration: configuration), |
| 74 | + on: eventLoopGroup |
| 75 | +) |
| 76 | +defer { pools.shutdown() } |
| 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`. |
12 | 105 |
|
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]. |
| 106 | +```swift |
| 107 | +let postgres = pool.database(logger: ...) // PostgresDatabase |
| 108 | +let rows = try postgres.simpleQuery("SELECT version();").wait() |
| 109 | +``` |
16 | 110 |
|
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. |
| 111 | +Visit [PostgresNIO's docs] for more information on using `PostgresDatabase`. |
18 | 112 |
|
19 |
| -> Tip: A FluentKit driver for PostgreSQL is provided by the [FluentPostgresDriver] package. |
| 113 | +### SQLDatabase |
20 | 114 |
|
21 |
| -## Version Support |
| 115 | +A `PostgresDatabase` can be used to create an instance of `SQLDatabase`. |
22 | 116 |
|
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. |
| 117 | +```swift |
| 118 | +let sql = postgres.sql() // SQLDatabase |
| 119 | +let planets = try sql.select().column("*").from("planets").all().wait() |
| 120 | +``` |
24 | 121 |
|
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. |
| 122 | +Visit [SQLKit's docs] for more information on using `SQLDatabase`. |
26 | 123 |
|
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 |
| 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 |
32 | 129 | [PostgresClient]: https://api.vapor.codes/postgresnio/documentation/postgresnio/postgresclient
|
| 130 | +[SwiftNIO's documentation]: https://swiftpackageindex.com/apple/swift-nio/documentation/nio |
0 commit comments