Make database connection lazy and adjust config execution order

This commit is contained in:
2025-08-21 15:00:10 +03:00
parent f4fefb3391
commit d26587cfc3
8 changed files with 129 additions and 173 deletions

View File

@@ -17,11 +17,6 @@ import DataLiteCore
///
/// - ``DatabaseServiceKeyProvider``
/// - ``keyProvider``
/// - ``applyKeyProvider()``
///
/// ### Connection Management
///
/// - ``reconnect()``
///
/// ### Database Operations
///
@@ -47,24 +42,6 @@ public protocol DatabaseServiceProtocol: AnyObject, Sendable {
/// encountered while applying a key.
var keyProvider: DatabaseServiceKeyProvider? { get set }
/// Applies the encryption key from the current provider.
///
/// Calls the configured ``keyProvider`` to obtain a key and applies
/// it to the active connection. If the key is unavailable or an
/// error occurs while applying it, the method throws.
///
/// - Throws: An error if the key cannot be retrieved or applied.
func applyKeyProvider() throws
/// Reopens the database connection.
///
/// Creates a new connection using the provider and applies the
/// encryption key if ``keyProvider`` is set. Typically used when
/// the previous connection has become invalid.
///
/// - Throws: An error if the new connection cannot be created or the key cannot be applied.
func reconnect() throws
/// Executes the given closure with an active connection.
///
/// The closure receives the connection and may perform any
@@ -73,7 +50,7 @@ public protocol DatabaseServiceProtocol: AnyObject, Sendable {
/// - Parameter closure: The closure that accepts a connection.
/// - Returns: The value returned by the closure.
/// - Throws: An error if one occurs during closure execution.
func perform<T>(_ closure: Perform<T>) rethrows -> T
func perform<T>(_ closure: Perform<T>) throws -> T
/// Executes the given closure within a transaction.
///
@@ -86,5 +63,5 @@ public protocol DatabaseServiceProtocol: AnyObject, Sendable {
/// - closure: The closure that accepts a connection.
/// - Returns: The value returned by the closure.
/// - Throws: An error if one occurs during closure execution.
func perform<T>(in transaction: TransactionType, closure: Perform<T>) rethrows -> T
func perform<T>(in transaction: TransactionType, closure: Perform<T>) throws -> T
}