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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.52.0"),
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.16.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.58.0"),
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.18.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.23.1"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.0.0"),
Expand Down
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,7 @@ let config = PostgresConnection.Configuration(
)
```

A connection must be created on a SwiftNIO `EventLoop`. In most server use cases, an
`EventLoopGroup` is created at app startup and closed during app shutdown.

```swift
import NIOPosix

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

// Much later
try await eventLoopGroup.shutdownGracefully()
```

A [`Logger`] is also required.
To create a connection we need a [`Logger`], that is used to log connection background events.

```swift
import Logging
Expand All @@ -91,10 +79,8 @@ Now we can put it together:

```swift
import PostgresNIO
import NIOPosix
import Logging

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let logger = Logger(label: "postgres-logger")

let config = PostgresConnection.Configuration(
Expand All @@ -107,17 +93,13 @@ let config = PostgresConnection.Configuration(
)

let connection = try await PostgresConnection.connect(
on: eventLoopGroup.next(),
configuration: config,
id: 1,
logger: logger
)

// Close your connection once done
try await connection.close()

// Shutdown the EventLoopGroup, once all connections are closed.
try await eventLoopGroup.shutdownGracefully()
```

#### Querying
Expand Down
21 changes: 19 additions & 2 deletions Sources/PostgresNIO/Connection/PostgresConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ extension PostgresConnection {
/// Creates a new connection to a Postgres server.
///
/// - Parameters:
/// - eventLoop: The `EventLoop` the request shall be created on
/// - eventLoop: The `EventLoop` the connection shall be created on.
/// - configuration: A ``Configuration`` that shall be used for the connection
/// - connectionID: An `Int` id, used for metadata logging
/// - logger: A logger to log background events into
/// - Returns: An established ``PostgresConnection`` asynchronously that can be used to run queries.
public static func connect(
on eventLoop: EventLoop,
on eventLoop: EventLoop = PostgresConnection.defaultEventLoopGroup.any(),
configuration: PostgresConnection.Configuration,
id connectionID: ID,
logger: Logger
Expand Down Expand Up @@ -661,3 +661,20 @@ extension EventLoopFuture {
}
}
}

extension PostgresConnection {
/// Returns the default `EventLoopGroup` singleton, automatically selecting the best for the platform.
///
/// This will select the concrete `EventLoopGroup` depending which platform this is running on.
public static var defaultEventLoopGroup: EventLoopGroup {
#if canImport(Network)
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
return NIOTSEventLoopGroup.singleton
} else {
return MultiThreadedEventLoopGroup.singleton
}
#else
return MultiThreadedEventLoopGroup.singleton
#endif
}
}