Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ let package = Package(
.library(name: "FeatherDatabasePostgres", targets: ["FeatherDatabasePostgres"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-log", from: "1.14.0"),
.package(url: "https://github.com/vapor/postgres-nio", from: "1.27.0"),
.package(url: "https://github.com/feather-framework/feather-database", exact: "1.0.0-rc.1"),
.package(url: "https://github.com/feather-framework/feather-database", exact: "1.0.0-rc.2"),
// [docc-plugin-placeholder]
],
targets: [
Expand Down
112 changes: 52 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# Feather Database Postgres
# Feather Database Postgres

Postgres driver implementation for the abstract [Feather Database](https://github.com/feather-framework/feather-database) Swift API package.

[
![Release: 1.0.0-rc.1](https://img.shields.io/badge/Release-1%2E0%2E0--rc%2E1-F05138)
](
https://github.com/feather-framework/feather-database-postgres/releases/tag/1.0.0-rc.1
)
[![Release: 1.0.0-rc.2](https://img.shields.io/badge/Release-1%2E0%2E0--rc%2E2-F05138)](https://github.com/feather-framework/feather-database-postgres/releases/tag/1.0.0-rc.2)

## Features

Expand Down Expand Up @@ -37,7 +33,7 @@ Postgres driver implementation for the abstract [Feather Database](https://githu
Add the dependency to your `Package.swift`:

```swift
.package(url: "https://github.com/feather-framework/feather-database-postgres", exact: "1.0.0-rc.1"),
.package(url: "https://github.com/feather-framework/feather-database-postgres", exact: "1.0.0-rc.2"),
```

Then add `FeatherDatabasePostgres` to your target dependencies:
Expand All @@ -50,11 +46,7 @@ Then add `FeatherDatabasePostgres` to your target dependencies:

API documentation is available at the link below:

[
![DocC API documentation](https://img.shields.io/badge/DocC-API_documentation-F05138)
](
https://feather-framework.github.io/feather-database-postgres/
)
[![DocC API documentation](https://img.shields.io/badge/DocC-API_documentation-F05138)](https://feather-framework.github.io/feather-database-postgres/)

Here is a brief example:

Expand All @@ -65,60 +57,60 @@ import PostgresNIO
import FeatherDatabase
import FeatherDatabasePostgres

var logger = Logger(label: "example")
logger.logLevel = .info

let finalCertPath = URL(fileURLWithPath: "/path/to/ca.pem")
var tlsConfig = TLSConfiguration.makeClientConfiguration()
let rootCert = try NIOSSLCertificate.fromPEMFile(finalCertPath)
tlsConfig.trustRoots = .certificates(rootCert)
tlsConfig.certificateVerification = .fullVerification

let client = PostgresClient(
configuration: .init(
host: "127.0.0.1",
port: 5432,
username: "postgres",
password: "postgres",
database: "postgres",
tls: .require(tlsConfig)
),
backgroundLogger: logger
)

let database = DatabaseClientPostgres(
client: client,
logger: logger
)

try await withThrowingTaskGroup(of: Void.self) { group in
// run the client as a service
group.addTask {
await client.run()
}
// execute some query
group.addTask {
let result = try await database.withConnection { connection in
try await connection.run(
query: #"""
SELECT
version() AS "version"
WHERE
1=\#(1);
"""#
)
try await withLogger(Logger(label: "example")) { _ in
let finalCertPath = URL(fileURLWithPath: "/path/to/ca.pem")
var tlsConfig = TLSConfiguration.makeClientConfiguration()
let rootCert = try NIOSSLCertificate.fromPEMFile(finalCertPath)
tlsConfig.trustRoots = .certificates(rootCert)
tlsConfig.certificateVerification = .fullVerification

let client = PostgresClient(
configuration: .init(
host: "127.0.0.1",
port: 5432,
username: "postgres",
password: "postgres",
database: "postgres",
tls: .require(tlsConfig)
),
backgroundLogger: Logger.current
)

let database = DatabaseClientPostgres(
client: client
)

try await withThrowingTaskGroup(of: Void.self) { group in
// run the client as a service
group.addTask {
await client.run()
}

for try await item in result {
let version = try item.decode(column: "version", as: String.self)
print(version)
// execute some query
group.addTask {
let result = try await database.withConnection { connection in
try await connection.run(
query: #"""
SELECT
version() AS "version"
WHERE
1=\#(1);
"""#
)
}

for try await item in result {
let version = try item.decode(column: "version", as: String.self)
print(version)
}
}
try await group.next()
group.cancelAll()
}
try await group.next()
group.cancelAll()
}
```

The package uses `Logger.current` from [swift-log](https://github.com/apple/swift-log) for database logging. Use `withLogger` to scope the logger for an operation; calls to `Logger.current` within that scope use the scoped logger.

## Other database drivers

The following database driver implementations are available for use:
Expand Down
27 changes: 11 additions & 16 deletions Sources/FeatherDatabasePostgres/DatabaseClientPostgres.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ public struct DatabaseClientPostgres: DatabaseClient {
public typealias Connection = DatabaseConnectionPostgres

var client: PostgresNIO.PostgresClient
let logger: Logger

/// Create a Postgres database client.
///
/// Use this initializer to provide an existing Postgres client.
/// - Parameters:
/// - Parameter:
/// - client: The underlying Postgres client.
/// - logger: The logger for database operations.
public init(
client: PostgresNIO.PostgresClient,
logger: Logger
client: PostgresNIO.PostgresClient
) {
self.client = client
self.logger = logger
}

// MARK: - database api
Expand All @@ -44,12 +40,10 @@ public struct DatabaseClientPostgres: DatabaseClient {
public func withConnection<T>(
_ closure: (Connection) async throws -> T,
) async throws(DatabaseError) -> T {
let logger = self.logger
let body: (PostgresConnection) async throws -> T = { connection in
try await closure(
DatabaseConnectionPostgres(
connection: connection,
logger: logger
connection: connection
)
)
}
Expand All @@ -75,20 +69,21 @@ public struct DatabaseClientPostgres: DatabaseClient {
public func withTransaction<T>(
_ closure: (Connection) async throws -> T,
) async throws(DatabaseError) -> T {
let logger = self.logger
let beginQuery = PostgresQuery(unsafeSQL: "BEGIN", binds: .init())
let commitQuery = PostgresQuery(unsafeSQL: "COMMIT", binds: .init())
let rollbackQuery = PostgresQuery(unsafeSQL: "ROLLBACK", binds: .init())

do {
return try await client.withConnection { connection in
let databaseConnection = DatabaseConnectionPostgres(
connection: connection,
logger: logger
connection: connection
)

do {
_ = try await connection.query(beginQuery, logger: logger)
_ = try await connection.query(
beginQuery,
logger: Logger.current
)
}
catch {
throw DatabaseError.transaction(
Expand All @@ -103,7 +98,7 @@ public struct DatabaseClientPostgres: DatabaseClient {
do {
_ = try await connection.query(
commitQuery,
logger: logger
logger: Logger.current
)
return result
}
Expand All @@ -113,7 +108,7 @@ public struct DatabaseClientPostgres: DatabaseClient {
do {
_ = try await connection.query(
rollbackQuery,
logger: logger
logger: Logger.current
)
}
catch {
Expand All @@ -133,7 +128,7 @@ public struct DatabaseClientPostgres: DatabaseClient {
do {
_ = try await connection.query(
rollbackQuery,
logger: logger
logger: Logger.current
)
}
catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import FeatherDatabase
import Logging
import PostgresNIO

extension DatabaseQuery {
Expand Down Expand Up @@ -45,7 +46,6 @@ public struct DatabaseConnectionPostgres: DatabaseConnection {
public typealias RowSequence = DatabaseRowSequencePostgres

var connection: PostgresConnection
public var logger: Logger

/// Execute a Postgres query on this connection.
///
Expand All @@ -63,7 +63,7 @@ public struct DatabaseConnectionPostgres: DatabaseConnection {
do {
let sequence = try await connection.query(
query.toPostgresQuery(),
logger: logger
logger: Logger.current
)

return try await handler(
Expand Down
Loading
Loading