21 lines
602 B
Swift
21 lines
602 B
Swift
import FluentKit
|
|
import SQLKit
|
|
|
|
extension Database {
|
|
public func transaction<T: Sendable>(_ closure: @escaping @Sendable (any SQLDatabase) async throws -> T) async throws -> T {
|
|
guard let database = self as? SQLDatabase else {
|
|
throw NoSQLDatabaseError (database: self)
|
|
}
|
|
|
|
return try await self.transaction { (_: any Database) in
|
|
try await closure (database)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct NoSQLDatabaseError: Error, CustomDebugStringConvertible {
|
|
let database: any Database
|
|
|
|
var debugDescription: String { "\(database) is not a `SQLDatabase`" }
|
|
}
|