forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresConnection+Connect.swift
35 lines (31 loc) · 1.2 KB
/
PostgresConnection+Connect.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
35
import NIOCore
import NIOSSL
import Logging
extension PostgresConnection {
public static func connect(
to socketAddress: SocketAddress,
tlsConfiguration: TLSConfiguration? = nil,
serverHostname: String? = nil,
logger: Logger = .init(label: "codes.vapor.postgres"),
on eventLoop: EventLoop
) -> EventLoopFuture<PostgresConnection> {
let coders = PSQLConnection.Configuration.Coders(
jsonEncoder: PostgresJSONEncoderWrapper(_defaultJSONEncoder),
jsonDecoder: PostgresJSONDecoderWrapper(_defaultJSONDecoder)
)
let configuration = PSQLConnection.Configuration(
connection: .resolved(address: socketAddress, serverName: serverHostname),
authentication: nil,
tlsConfiguration: tlsConfiguration,
coders: coders)
return PSQLConnection.connect(
configuration: configuration,
logger: logger,
on: eventLoop
).map { connection in
PostgresConnection(underlying: connection, logger: logger)
}.flatMapErrorThrowing { error in
throw error.asAppropriatePostgresError
}
}
}