forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresConnectionSource.swift
34 lines (31 loc) · 1.17 KB
/
PostgresConnectionSource.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public struct PostgresConnectionSource: ConnectionPoolSource {
public var eventLoop: EventLoop
public let configuration: PostgresConfiguration
public init(configuration: PostgresConfiguration, on eventLoop: EventLoop) {
self.configuration = configuration
self.eventLoop = eventLoop
}
public func makeConnection() -> EventLoopFuture<PostgresConnection> {
let address: SocketAddress
do {
address = try self.configuration.address()
} catch {
return self.eventLoop.makeFailedFuture(error)
}
return PostgresConnection.connect(
to: address,
tlsConfiguration: self.configuration.tlsConfiguration,
on: self.eventLoop
).flatMap { conn in
return conn.authenticate(
username: self.configuration.username,
database: self.configuration.database,
password: self.configuration.password
).flatMapError { error in
return conn.close()
.flatMapThrowing { throw error }
}.map { conn }
}
}
}
extension PostgresConnection: ConnectionPoolItem { }