Skip to content

Commit dc0d053

Browse files
committed
Silence API breakage complaints by doing it the even uglier way
1 parent d889fe8 commit dc0d053

File tree

2 files changed

+164
-13
lines changed

2 files changed

+164
-13
lines changed

Sources/FluentPostgresDriver/FluentPostgresConfiguration.swift

Lines changed: 163 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,157 @@ import NIOCore
66
import PostgresKit
77
import PostgresNIO
88

9+
extension DatabaseConfigurationFactory {
10+
/// Create a PostgreSQL database configuration from a URL string.
11+
///
12+
/// See ``PostgresKit/SQLPostgresConfiguration/init(url:)`` for the allowed URL format.
13+
///
14+
/// - Parameters:
15+
/// - urlString: The URL describing the connection, as a string.
16+
/// - maxConnectionsPerEventLoop: Maximum number of connections to open per event loop.
17+
/// - connectionPoolTimeout: Maximum time to wait for a connection to become available per request.
18+
/// - encodingContext: Encoding context to use for serializing data.
19+
/// - decodingContext: Decoding context to use for deserializing data.
20+
/// - sqlLogLevel: Level at which to log SQL queries.
21+
public static func postgres(
22+
url urlString: String,
23+
maxConnectionsPerEventLoop: Int = 1,
24+
connectionPoolTimeout: TimeAmount = .seconds(10),
25+
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder> = .default,
26+
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder> = .default,
27+
sqlLogLevel: Logger.Level = .debug
28+
) throws -> Self {
29+
.postgres(
30+
configuration: try .init(url: urlString),
31+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
32+
connectionPoolTimeout: connectionPoolTimeout,
33+
pruneInterval: nil,
34+
encodingContext: encodingContext,
35+
decodingContext: decodingContext,
36+
sqlLogLevel: sqlLogLevel
37+
)
38+
}
39+
40+
/// Create a PostgreSQL database configuration from a URL.
41+
///
42+
/// See ``PostgresKit/SQLPostgresConfiguration/init(url:)`` for the allowed URL format.
43+
///
44+
/// - Parameters:
45+
/// - url: The URL describing the connection.
46+
/// - maxConnectionsPerEventLoop: Maximum number of connections to open per event loop.
47+
/// - connectionPoolTimeout: Maximum time to wait for a connection to become available per request.
48+
/// - encodingContext: Encoding context to use for serializing data.
49+
/// - decodingContext: Decoding context to use for deserializing data.
50+
/// - sqlLogLevel: Level at which to log SQL queries.
51+
public static func postgres(
52+
url: URL,
53+
maxConnectionsPerEventLoop: Int = 1,
54+
connectionPoolTimeout: TimeAmount = .seconds(10),
55+
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder> = .default,
56+
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder> = .default,
57+
sqlLogLevel: Logger.Level = .debug
58+
) throws -> Self {
59+
.postgres(
60+
configuration: try .init(url: url),
61+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
62+
connectionPoolTimeout: connectionPoolTimeout,
63+
pruneInterval: nil,
64+
encodingContext: encodingContext,
65+
decodingContext: decodingContext,
66+
sqlLogLevel: sqlLogLevel
67+
)
68+
}
69+
70+
/// Create a PostgreSQL database configuration from lower-level configuration.
71+
///
72+
/// - Parameters:
73+
/// - configuration: A ``PostgresKit/SQLPostgresConfiguration`` describing the connection.
74+
/// - maxConnectionsPerEventLoop: Maximum number of connections to open per event loop.
75+
/// - connectionPoolTimeout: Maximum time to wait for a connection to become available per request.
76+
/// - encodingContext: Encoding context to use for serializing data.
77+
/// - decodingContext: Decoding context to use for deserializing data.
78+
/// - sqlLogLevel: Level at which to log SQL queries.
79+
public static func postgres(
80+
configuration: SQLPostgresConfiguration,
81+
maxConnectionsPerEventLoop: Int = 1,
82+
connectionPoolTimeout: TimeAmount = .seconds(10),
83+
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder>,
84+
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder>,
85+
sqlLogLevel: Logger.Level = .debug
86+
) -> Self {
87+
.postgres(
88+
configuration: configuration,
89+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
90+
connectionPoolTimeout: connectionPoolTimeout,
91+
pruneInterval: nil,
92+
encodingContext: encodingContext,
93+
decodingContext: decodingContext,
94+
sqlLogLevel: sqlLogLevel
95+
)
96+
}
97+
}
98+
99+
extension DatabaseConfigurationFactory {
100+
/// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)``
101+
/// with the `decodingContext` defaulted.
102+
public static func postgres(
103+
configuration: SQLPostgresConfiguration,
104+
maxConnectionsPerEventLoop: Int = 1,
105+
connectionPoolTimeout: TimeAmount = .seconds(10),
106+
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder>,
107+
sqlLogLevel: Logger.Level = .debug
108+
) -> Self {
109+
.postgres(
110+
configuration: configuration,
111+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
112+
connectionPoolTimeout: connectionPoolTimeout,
113+
pruneInterval: nil,
114+
encodingContext: encodingContext,
115+
decodingContext: .default,
116+
sqlLogLevel: sqlLogLevel
117+
)
118+
}
119+
120+
/// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)``
121+
/// with the `encodingContext` defaulted.
122+
public static func postgres(
123+
configuration: SQLPostgresConfiguration,
124+
maxConnectionsPerEventLoop: Int = 1,
125+
connectionPoolTimeout: TimeAmount = .seconds(10),
126+
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder>,
127+
sqlLogLevel: Logger.Level = .debug
128+
) -> Self {
129+
.postgres(
130+
configuration: configuration,
131+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
132+
connectionPoolTimeout: connectionPoolTimeout,
133+
pruneInterval: nil,
134+
encodingContext: .default,
135+
decodingContext: decodingContext,
136+
sqlLogLevel: sqlLogLevel
137+
)
138+
}
139+
140+
/// ``postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)``
141+
/// with both `encodingContext` and `decodingContext` defaulted.
142+
public static func postgres(
143+
configuration: SQLPostgresConfiguration,
144+
maxConnectionsPerEventLoop: Int = 1,
145+
connectionPoolTimeout: TimeAmount = .seconds(10),
146+
sqlLogLevel: Logger.Level = .debug
147+
) -> Self {
148+
.postgres(
149+
configuration: configuration,
150+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
151+
connectionPoolTimeout: connectionPoolTimeout,
152+
pruneInterval: nil,
153+
encodingContext: .default,
154+
decodingContext: .default,
155+
sqlLogLevel: sqlLogLevel
156+
)
157+
}
158+
}
159+
9160
extension DatabaseConfigurationFactory {
10161
/// Create a PostgreSQL database configuration from a URL string.
11162
///
@@ -26,12 +177,12 @@ extension DatabaseConfigurationFactory {
26177
url urlString: String,
27178
maxConnectionsPerEventLoop: Int = 1,
28179
connectionPoolTimeout: TimeAmount = .seconds(10),
29-
pruneInterval: TimeAmount? = nil,
180+
pruneInterval: TimeAmount?,
30181
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
31182
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder> = .default,
32183
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder> = .default,
33184
sqlLogLevel: Logger.Level = .debug
34-
) throws -> DatabaseConfigurationFactory {
185+
) throws -> Self {
35186
.postgres(
36187
configuration: try .init(url: urlString),
37188
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
@@ -63,12 +214,12 @@ extension DatabaseConfigurationFactory {
63214
url: URL,
64215
maxConnectionsPerEventLoop: Int = 1,
65216
connectionPoolTimeout: TimeAmount = .seconds(10),
66-
pruneInterval: TimeAmount? = nil,
217+
pruneInterval: TimeAmount?,
67218
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
68219
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder> = .default,
69220
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder> = .default,
70221
sqlLogLevel: Logger.Level = .debug
71-
) throws -> DatabaseConfigurationFactory {
222+
) throws -> Self {
72223
.postgres(
73224
configuration: try .init(url: url),
74225
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
@@ -98,12 +249,12 @@ extension DatabaseConfigurationFactory {
98249
configuration: SQLPostgresConfiguration,
99250
maxConnectionsPerEventLoop: Int = 1,
100251
connectionPoolTimeout: TimeAmount = .seconds(10),
101-
pruneInterval: TimeAmount? = nil,
252+
pruneInterval: TimeAmount?,
102253
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
103254
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder>,
104255
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder>,
105256
sqlLogLevel: Logger.Level = .debug
106-
) -> DatabaseConfigurationFactory {
257+
) -> Self {
107258
.init {
108259
FluentPostgresConfiguration(
109260
configuration: configuration,
@@ -138,11 +289,11 @@ extension DatabaseConfigurationFactory {
138289
configuration: SQLPostgresConfiguration,
139290
maxConnectionsPerEventLoop: Int = 1,
140291
connectionPoolTimeout: TimeAmount = .seconds(10),
141-
pruneInterval: TimeAmount? = nil,
292+
pruneInterval: TimeAmount?,
142293
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
143294
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder>,
144295
sqlLogLevel: Logger.Level = .debug
145-
) -> DatabaseConfigurationFactory {
296+
) -> Self {
146297
.postgres(
147298
configuration: configuration,
148299
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
@@ -161,11 +312,11 @@ extension DatabaseConfigurationFactory {
161312
configuration: SQLPostgresConfiguration,
162313
maxConnectionsPerEventLoop: Int = 1,
163314
connectionPoolTimeout: TimeAmount = .seconds(10),
164-
pruneInterval: TimeAmount? = nil,
315+
pruneInterval: TimeAmount?,
165316
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
166317
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder>,
167318
sqlLogLevel: Logger.Level = .debug
168-
) -> DatabaseConfigurationFactory {
319+
) -> Self {
169320
.postgres(
170321
configuration: configuration,
171322
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
@@ -184,10 +335,10 @@ extension DatabaseConfigurationFactory {
184335
configuration: SQLPostgresConfiguration,
185336
maxConnectionsPerEventLoop: Int = 1,
186337
connectionPoolTimeout: TimeAmount = .seconds(10),
187-
pruneInterval: TimeAmount? = nil,
338+
pruneInterval: TimeAmount?,
188339
maxIdleTimeBeforePruning: TimeAmount = .seconds(120),
189340
sqlLogLevel: Logger.Level = .debug
190-
) -> DatabaseConfigurationFactory {
341+
) -> Self {
191342
.postgres(
192343
configuration: configuration,
193344
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ extension DatabaseConfigurationFactory {
298298
subconfig: String,
299299
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder> = .default,
300300
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder> = .default
301-
) -> DatabaseConfigurationFactory {
301+
) -> Self {
302302
let baseSubconfig = SQLPostgresConfiguration(
303303
hostname: env("POSTGRES_HOSTNAME_\(subconfig)") ?? "localhost",
304304
port: env("POSTGRES_PORT_\(subconfig)").flatMap(Int.init) ?? SQLPostgresConfiguration.ianaPortNumber,

0 commit comments

Comments
 (0)