forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLDatabase.swift
35 lines (30 loc) · 1.18 KB
/
PostgreSQLDatabase.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
import Async
/// Creates connections to an identified PostgreSQL database.
public final class PostgreSQLDatabase: Database {
/// This database's configuration.
public let config: PostgreSQLDatabaseConfig
/// Creates a new `PostgreSQLDatabase`.
public init(config: PostgreSQLDatabaseConfig) {
self.config = config
}
/// See `Database.makeConnection()`
public func makeConnection(using connectionConfig: PostgreSQLConnectionConfig, on worker: Worker) -> Future<PostgreSQLConnection> {
do {
let client = try PostgreSQLConnection.connect(hostname: config.hostname, port: config.port, on: worker)
return client.authenticate(username: config.username).transform(to: client)
} catch {
return Future(error: error)
}
}
}
/// A connection created by a `PostgreSQLDatabase`.
extension PostgreSQLConnection: DatabaseConnection {
/// See `DatabaseConnection.Config`
public typealias Config = PostgreSQLConnectionConfig
}
extension DatabaseIdentifier {
/// Default identifier for `PostgreSQLDatabase`.
public static var psql: DatabaseIdentifier<PostgreSQLDatabase> {
return .init("psql")
}
}