-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7632411
commit 2fb0ae6
Showing
11 changed files
with
334 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import PostgresNIO | ||
import Foundation | ||
|
||
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
enum Birthday { | ||
static func main() async throws { | ||
// 1. Create a configuration to match server's parameters | ||
let config = PostgresClient.Configuration( | ||
host: "localhost", | ||
port: 5432, | ||
username: "test_username", | ||
password: "test_password", | ||
database: "test_database", | ||
tls: .disable | ||
) | ||
|
||
// 2. Create a client | ||
let client = PostgresClient(configuration: config) | ||
|
||
// 3. Run the client | ||
try await withThrowingTaskGroup(of: Void.self) { taskGroup in | ||
taskGroup.addTask { | ||
await client.run() // !important | ||
} | ||
|
||
// 4. Create a friends table to store data into | ||
try await client.query(""" | ||
CREATE TABLE IF NOT EXISTS "friends" ( | ||
id SERIAL PRIMARY KEY, | ||
given_name TEXT, | ||
last_name TEXT, | ||
birthday TIMESTAMP WITH TIME ZONE | ||
) | ||
""" | ||
) | ||
|
||
// 5. Create a Swift friend representation | ||
struct Friend { | ||
var firstName: String | ||
var lastName: String | ||
var birthday: Date | ||
} | ||
|
||
// 6. Create John Appleseed with special birthday | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yyyy-MM-dd" | ||
let johnsBirthday = dateFormatter.date(from: "1960-09-26")! | ||
let friend = Friend(firstName: "Hans", lastName: "Müller", birthday: johnsBirthday) | ||
|
||
// 7. Store friend into the database | ||
try await client.query(""" | ||
INSERT INTO "friends" (given_name, last_name, birthday) | ||
VALUES | ||
(\(friend.firstName), \(friend.lastName), \(friend.birthday)); | ||
""" | ||
) | ||
|
||
// 8. Query database for the friend we just inserted | ||
let rows = try await client.query(""" | ||
SELECT id, given_name, last_name, birthday FROM "friends" WHERE given_name = \(friend.firstName) | ||
""" | ||
) | ||
|
||
// 9. Iterate the returned rows, decoding the rows into Swift primitives | ||
for try await (id, firstName, lastName, birthday) in rows.decode((Int, String, String, Date).self) { | ||
print("\(id) | \(firstName) \(lastName), \(birthday)") | ||
} | ||
|
||
// 10. Shutdown the client, by cancelling its run method, through cancelling the taskGroup. | ||
taskGroup.cancelAll() | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import PostgresNIO | ||
import struct Foundation.UUID | ||
|
||
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
enum Runner { | ||
static func main() async throws { | ||
|
||
// snippet.configuration | ||
let config = PostgresClient.Configuration( | ||
host: "localhost", | ||
port: 5432, | ||
username: "my_username", | ||
password: "my_password", | ||
database: "my_database", | ||
tls: .disable | ||
) | ||
// snippet.end | ||
|
||
// snippet.makeClient | ||
let client = PostgresClient(configuration: config) | ||
// snippet.end | ||
|
||
} | ||
|
||
static func runAndCancel(client: PostgresClient) async { | ||
// snippet.run | ||
await withTaskGroup(of: Void.self) { taskGroup in | ||
taskGroup.addTask { | ||
await client.run() // !important | ||
} | ||
|
||
// You can use the client while the `client.run()` method is not cancelled. | ||
|
||
// To shutdown the client, cancel its run method, by cancelling the taskGroup. | ||
taskGroup.cancelAll() | ||
} | ||
// snippet.end | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# PostgreSQL data types | ||
|
||
Translate Swift data types to Postgres data types and vica versa. Learn how to write translations | ||
for your own custom Swift types. | ||
|
||
## Topics | ||
|
||
### Essentials | ||
|
||
- ``PostgresCodable`` | ||
- ``PostgresDataType`` | ||
- ``PostgresFormat`` | ||
- ``PostgresNumeric`` | ||
|
||
### Encoding | ||
|
||
- ``PostgresEncodable`` | ||
- ``PostgresNonThrowingEncodable`` | ||
- ``PostgresDynamicTypeEncodable`` | ||
- ``PostgresThrowingDynamicTypeEncodable`` | ||
- ``PostgresArrayEncodable`` | ||
- ``PostgresRangeEncodable`` | ||
- ``PostgresRangeArrayEncodable`` | ||
- ``PostgresEncodingContext`` | ||
|
||
### Decoding | ||
|
||
- ``PostgresDecodable`` | ||
- ``PostgresArrayDecodable`` | ||
- ``PostgresRangeDecodable`` | ||
- ``PostgresRangeArrayDecodable`` | ||
- ``PostgresDecodingContext`` | ||
|
||
### JSON | ||
|
||
- ``PostgresJSONEncoder`` | ||
- ``PostgresJSONDecoder`` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Deprecations | ||
|
||
`PostgresNIO` follows SemVer 2.0.0. Learn which APIs are considered deprecated and how to migrate to | ||
their replacements. | ||
|
||
``PostgresNIO`` reached 1.0 in April 2020. Since then the maintainers have been hard at work to | ||
guarantee API stability. However as the Swift and Swift on server ecosystem have matured approaches | ||
have changed. The introduction of structured concurrency changed what developers expect from a | ||
modern Swift library. Because of this ``PostgresNIO`` added various APIs that embrace the new Swift | ||
patterns. This means however, that PostgresNIO still offers APIs that have fallen out of favor. | ||
Those are documented here. All those APIs will be removed once the maintainers release the next | ||
major version. The maintainers recommend all adopters to move of those APIs sooner rather than | ||
later. | ||
|
||
## Topics | ||
|
||
### Migrate of deprecated APIs | ||
|
||
- <doc:migrations> | ||
|
||
### Deprecated APIs | ||
|
||
These types are already deprecated or will be deprecated in the near future. All of them will be | ||
removed from the public API with the next major release. | ||
|
||
- ``PostgresDatabase`` | ||
- ``PostgresData`` | ||
- ``PostgresDataConvertible`` | ||
- ``PostgresQueryResult`` | ||
- ``PostgresJSONCodable`` | ||
- ``PostgresJSONBCodable`` | ||
- ``PostgresMessageEncoder`` | ||
- ``PostgresMessageDecoder`` | ||
- ``PostgresRequest`` | ||
- ``PostgresMessage`` | ||
- ``PostgresMessageType`` | ||
- ``PostgresFormatCode`` | ||
- ``PostgresListenContext`` | ||
- ``PreparedQuery`` | ||
- ``SASLAuthenticationManager`` | ||
- ``SASLAuthenticationMechanism`` | ||
- ``SASLAuthenticationError`` | ||
- ``SASLAuthenticationStepResult`` |
Oops, something went wrong.