Moved the PostgreSQL TLS context building to the Service initialiser in the Persistence package.

This commit is contained in:
2026-08-05 01:36:19 +02:00
parent e51f09c66f
commit 0796aa314e
5 changed files with 50 additions and 29 deletions
@@ -2,16 +2,25 @@ import FluentPostgresDriver
import FluentSQLiteDriver
import HummingbirdFluent
import Logging
import PostgresNIO
/// A factory building the `Fluent` service the application persists through.
///
/// Built once around the driver the executable picks at startup and called as a function to produce the configured service: `let fluent = service()`.
public struct Service: Sendable {
// MARK: Enumerations
/// The persistence backend resolved at construction, with the PostgreSQL TLS mode already built.
private enum Backend {
case postgres(Configuration, PostgresConnection.Configuration.TLS)
case inMemory
}
// MARK: Properties
/// The persistence backend to register.
private let driver: Driver
/// The resolved persistence backend to register.
private let backend: Backend
/// The logger the database emits through.
private let logger: Logger
@@ -19,14 +28,27 @@ public struct Service: Sendable {
// MARK: Initializers
/// Creates a factory for a `Fluent` service backed by the given driver.
///
/// The TLS context for the PostgreSQL backend is built here, once the factory holds only resolved configuration, so producing the
/// service afterwards cannot fail.
/// - Parameters:
/// - driver: the persistence backend to register.
/// - logger: the logger the database emits through.
/// - Throws: an error when the TLS context for the PostgreSQL backend cannot be built.
public init(
driver: Driver,
logger: Logger
) {
self.driver = driver
) throws {
switch driver {
case .postgres(let configuration):
self.backend = .postgres(
configuration,
try configuration.tls.postgresTLS()
)
case .inMemory:
self.backend = .inMemory
}
self.logger = logger
}
@@ -37,15 +59,14 @@ public struct Service: Sendable {
/// The selected backend is registered as the *default* database, so repositories resolve it with a plain `fluent.db()` and stay agnostic of which
/// driver is in use. The returned service is not yet running; add it to the application's service group (`app.addServices(_:)`) so it starts and shuts
/// its connection pool down alongside the server.
/// - Throws: an error when the TLS context for the PostgreSQL backend cannot be built.
/// - Returns: the configured `Fluent` service, ready to be added to the service group.
public func callAsFunction() throws -> Fluent {
public func callAsFunction() -> Fluent {
let fluent = Fluent(
logger: logger
)
switch driver {
case .postgres(let configuration):
switch backend {
case .postgres(let configuration, let tls):
fluent.databases.use(
.postgres(
configuration: .init(
@@ -54,7 +75,7 @@ public struct Service: Sendable {
username: configuration.username,
password: configuration.password,
database: configuration.name,
tls: try configuration.tls.postgresTLS()
tls: tls
),
maxConnectionsPerEventLoop: configuration.maxConnectionsPerEventLoop
),