-
-
Notifications
You must be signed in to change notification settings - Fork 89
Docs: DocC prepared statements examples + PostgresCodable schema hints; fix tuple decoding in test #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Added a new section on manual query construction using `PostgresBindings` in the README.md. - Introduced a new `PostgresCodable` documentation file detailing how to encode and decode custom Swift types. - Enhanced the `PostgresQuery` documentation with examples of string interpolation and dynamic query building. - Updated `.gitignore` to include `.derivedData`. - Added integration tests for JSONB encoding and decoding functionality in `PostgresClientTests.swift`.
- Updated `decode` methods in `PostgresRandomAccessRow` to be public for better accessibility. - Modified documentation examples to utilize `makeRandomAccess()` for decoding rows, ensuring consistency and clarity. - Added new examples demonstrating the use of Codable structs with JSONB in the documentation. - Introduced integration tests for JSONB encoding/decoding functionality, validating the round-trip of nested Codable structs.
…ansaction; tests: decode multi-column rows via tuple in PostgresCodable test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several initial nits. I haven't bothered including notes on every single line where double-backticks were missing or - Note:
-style callouts were used instead of > Note:
, it'd take me a lot longer to do that in the PR interface than it'd take anyone to just fix it in Xcode or such.
/// - Throws: The error of the decoding implementation. See also `PSQLDecodable` protocol for this. | ||
/// - Returns: The decoded value of Type T. | ||
func decode<T: PostgresDecodable, JSONDecoder: PostgresJSONDecoder>( | ||
public func decode<T: PostgresDecodable, JSONDecoder: PostgresJSONDecoder>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
/// - Throws: The error of the decoding implementation. See also `PSQLDecodable` protocol for this. | ||
/// - Returns: The decoded value of Type T. | ||
func decode<T: PostgresDecodable, JSONDecoder: PostgresJSONDecoder>( | ||
public func decode<T: PostgresDecodable, JSONDecoder: PostgresJSONDecoder>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
/// } | ||
/// ``` | ||
/// | ||
/// - Note: `PostgresNonThrowingEncodable` is a variant that doesn't throw, allowing usage without `try`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// - Note: `PostgresNonThrowingEncodable` is a variant that doesn't throw, allowing usage without `try`. | |
/// > Note: ``PostgresNonThrowingEncodable`` is a variant that doesn't throw, allowing usage without `try`. |
/// A type that can decode itself from a postgres wire binary representation. | ||
/// | ||
/// If you want to conform a type to PostgresDecodable you must implement the decode method. | ||
/// Conform your custom types to `PostgresDecodable` to enable them to be decoded from query results. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Conform your custom types to `PostgresDecodable` to enable them to be decoded from query results. | |
/// Conform your custom types to ``PostgresDecodable`` to enable them to be decoded from query results. |
/// | ||
/// ## Conforming Built-in Types | ||
/// | ||
/// Many standard Swift types already conform to `PostgresDecodable`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Many standard Swift types already conform to `PostgresDecodable`: | |
/// Many standard Swift types already conform to ``PostgresDecodable``: |
/// // Returns nil if the database value is NULL | ||
/// ``` | ||
/// | ||
/// - Note: The `_DecodableType` associated type is an implementation detail for Optional handling. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// - Note: The `_DecodableType` associated type is an implementation detail for Optional handling. | |
/// > Note: The `_DecodableType` associated type is an implementation detail for `Optional` handling. |
/// - Note: String interpolation is the recommended approach for simple queries as it automatically handles parameter counting and binding. | ||
/// - Warning: Always use parameter binding for user input. Never concatenate user input directly into SQL strings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// - Note: String interpolation is the recommended approach for simple queries as it automatically handles parameter counting and binding. | |
/// - Warning: Always use parameter binding for user input. Never concatenate user input directly into SQL strings. | |
/// > Note: String interpolation is the recommended approach for simple queries as it automatically handles parameter counting and binding. | |
/// | |
/// > Warning: Always use parameter binding for user input. Never concatenate user input directly into SQL strings. |
/// - SeeAlso: `PostgresBindings` for more details on manual binding construction. | ||
/// - SeeAlso: `PostgresClient` for connection pool management and query execution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// - SeeAlso: `PostgresBindings` for more details on manual binding construction. | |
/// - SeeAlso: `PostgresClient` for connection pool management and query execution. | |
/// - SeeAlso: ``PostgresBindings`` for more details on manual binding construction. | |
/// - SeeAlso: ``PostgresClient`` for connection pool management and query execution. |
.gitignore
Outdated
Package.resolved | ||
.swiftpm | ||
Tests/LinuxMain.swift | ||
.derivedData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was an accident as I was unable to use DocC to break a cache. I will remove it.
var tlsConfiguration = TLSConfiguration.makeClientConfiguration() | ||
tlsConfiguration.certificateVerification = .none | ||
var clientConfig = PostgresClient.Configuration( | ||
host: env("POSTGRES_HOSTNAME") ?? "localhost", | ||
port: env("POSTGRES_PORT").flatMap({ Int($0) }) ?? 5432, | ||
username: env("POSTGRES_USER") ?? "test_username", | ||
password: env("POSTGRES_PASSWORD") ?? "test_password", | ||
database: env("POSTGRES_DB") ?? "test_database", | ||
tls: .prefer(tlsConfiguration) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var tlsConfiguration = TLSConfiguration.makeClientConfiguration() | |
tlsConfiguration.certificateVerification = .none | |
var clientConfig = PostgresClient.Configuration( | |
host: env("POSTGRES_HOSTNAME") ?? "localhost", | |
port: env("POSTGRES_PORT").flatMap({ Int($0) }) ?? 5432, | |
username: env("POSTGRES_USER") ?? "test_username", | |
password: env("POSTGRES_PASSWORD") ?? "test_password", | |
database: env("POSTGRES_DB") ?? "test_database", | |
tls: .prefer(tlsConfiguration) | |
var clientConfig = PostgresClient.Configuration( | |
host: env("POSTGRES_HOSTNAME") ?? "localhost", | |
port: env("POSTGRES_PORT").flatMap({ Int($0) }) ?? 5432, | |
username: env("POSTGRES_USER") ?? "test_username", | |
password: env("POSTGRES_PASSWORD") ?? "test_password", | |
database: env("POSTGRES_DB") ?? "test_database", | |
tls: .disable |
Don't encourage people to disable certificate verification. Match the existing test configuration by disabling TLS.
Great I made a bunch of commits to address these. |
PostgresPreparedStatement
and demonstrates usage with.query
and.withTransaction
.PostgresCodable
docs with minimal SQL schemas for each code snippet so readers know exactly what tables/columns are expected.Sources/PostgresNIO/Docs.docc/prepared-statement.md
: New end‑to‑end examplesInsertUser
andLoadUser
prepared statements.query
callswithTransaction
for atomic sequencesSources/PostgresNIO/Docs.docc/postgres-codable.md
: Add inline SQL schema hints for the examplesUser
codable,Company
(JSONB),location
(POINT), and enum/raw‑value examplesTests/IntegrationTests/PostgresCodableTests.swift
: Decode multi‑column rows via tuple and then construct the model (avoids unsupported direct struct decoding)