-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathPostgresConnectionSource.swift
45 lines (39 loc) · 1.31 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
35
36
37
38
39
40
41
42
43
44
45
import AsyncKit
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOSSL
import PostgresNIO
import SQLKit
public struct PostgresConnectionSource: ConnectionPoolSource {
public let sqlConfiguration: SQLPostgresConfiguration
private static let idGenerator = NIOLockedValueBox<Int>(0)
public init(sqlConfiguration: SQLPostgresConfiguration) {
self.sqlConfiguration = sqlConfiguration
}
public func makeConnection(
logger: Logger,
on eventLoop: any EventLoop
) -> EventLoopFuture<PostgresConnection> {
let connectionFuture = PostgresConnection.connect(
on: eventLoop,
configuration: self.sqlConfiguration.coreConfiguration,
id: Self.idGenerator.withLockedValue {
$0 += 1
return $0
},
logger: logger
)
if let searchPath = self.sqlConfiguration.searchPath {
return connectionFuture.flatMap { conn in
conn.sql(queryLogLevel: nil)
.raw("SET search_path TO \(idents: searchPath, joinedBy: ",")")
.run()
.map { _ in conn }
}
} else {
return connectionFuture
}
}
}
extension PostgresNIO.PostgresConnection: AsyncKit.ConnectionPoolItem {}