Skip to content

Commit b347744

Browse files
committed
Upgrade DatabaseIdentifier 🍓 and implement conveniences
1 parent 525c917 commit b347744

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

Sources/MySQLBridge/Extensions/Bridges+Application.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import Bridges
99

1010
extension BridgesApplication {
1111
public var mysql: MySQLBridge {
12-
.init(bridges.bridge(to: _MySQLBridge.self, on: eventLoopGroup.next()))
12+
.init(bridges.bridge(to: MBR.self, on: eventLoopGroup.next()))
1313
}
1414
}
1515

1616
extension BridgesRequest {
1717
public var mysql: MySQLBridge {
18-
.init(bridgesApplication.bridges.bridge(to: _MySQLBridge.self, on: eventLoop))
18+
.init(bridgesApplication.bridges.bridge(to: MBR.self, on: eventLoop))
1919
}
2020
}
2121

2222
import NIO
2323
import Logging
2424

25-
extension _MySQLBridge {
25+
extension MBR {
2626
public static func create(eventLoopGroup: EventLoopGroup, logger: Logger) -> AnyBridge {
27-
_MySQLBridge(eventLoopGroup: eventLoopGroup, logger: logger)
27+
MBR(eventLoopGroup: eventLoopGroup, logger: logger)
2828
}
2929
}

Sources/MySQLBridge/Extensions/DatabaseIdentifier+Initialization.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ import Bridges
1111
extension DatabaseIdentifier {
1212
/// Initialize identifier based on `MYSQL_DB` environment variable
1313
public static var mysqlEnvironment: DatabaseIdentifier {
14-
.init(name: ProcessInfo.processInfo.environment["MYSQL_DB"], host: .mysqlEnvironment, maxConnectionsPerEventLoop: 1)
14+
MySQLDatabaseIdentifier(name: ProcessInfo.processInfo.environment["MYSQL_DB"], host: .mysqlEnvironment, maxConnectionsPerEventLoop: 1)
1515
}
16+
17+
public static func mysql(name: String? = ProcessInfo.processInfo.environment["MYSQL_DB"], host: DatabaseHost, maxConnectionsPerEventLoop: Int = 1) -> DatabaseIdentifier {
18+
MySQLDatabaseIdentifier(name: name, host: host, maxConnectionsPerEventLoop: maxConnectionsPerEventLoop)
19+
}
20+
}
1621

17-
public init?(url: URL, maxConnectionsPerEventLoop: Int = 1) {
22+
public class MySQLDatabaseIdentifier: DatabaseIdentifier, MySQLDatabaseIdentifiable {
23+
public typealias B = MBR
24+
25+
public convenience init?(url: URL, maxConnectionsPerEventLoop: Int = 1) {
1826
guard let host = DatabaseHost(url: url) else {
1927
return nil
2028
}
2129
self.init(name: url.path.split(separator: "/").last.flatMap(String.init), host: host, maxConnectionsPerEventLoop: maxConnectionsPerEventLoop)
2230
}
31+
32+
public func all<T>(_ table: T.Type, on bridges: AnyBridgesObject) -> EventLoopFuture<[T]> where T : Table {
33+
MySQLBridge(bridges.bridges.bridge(to: B.self, on: bridges.eventLoop)).connection(to: self) { conn in
34+
T.select.execute(on: conn).all(decoding: T.self)
35+
}
36+
}
37+
38+
public func first<T>(_ table: T.Type, on bridges: AnyBridgesObject) -> EventLoopFuture<T?> where T : Table {
39+
MySQLBridge(bridges.bridges.bridge(to: B.self, on: bridges.eventLoop)).connection(to: self) { conn in
40+
T.select.execute(on: conn).first(decoding: T.self)
41+
}
42+
}
2343
}

Sources/MySQLBridge/MySQLBridge.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import AsyncKit
44
import Bridges
55
import Logging
66

7-
public struct MySQLBridge {
8-
let context: BridgeWithContext<_MySQLBridge>
7+
public struct MySQLBridge: ContextBridgeable {
8+
public let context: BridgeWithContext<MBR>
99

10-
init (_ context: BridgeWithContext<_MySQLBridge>) {
10+
init (_ context: BridgeWithContext<MBR>) {
1111
self.context = context
1212
}
1313

@@ -26,35 +26,35 @@ public struct MySQLBridge {
2626
}
2727

2828
public func migrator(for db: DatabaseIdentifier) -> Migrator {
29-
BridgeDatabaseMigrations<_MySQLBridge>(context.bridge, db: db)
29+
BridgeDatabaseMigrations<MBR>(context.bridge, db: db)
3030
}
3131
}
3232

33-
final class _MySQLBridge: Bridgeable {
34-
typealias Source = MySQLConnectionSource
35-
typealias Database = MySQLDatabase
36-
typealias Connection = MySQLConnection
33+
public final class MBR: Bridgeable {
34+
public typealias Source = MySQLConnectionSource
35+
public typealias Database = MySQLDatabase
36+
public typealias Connection = MySQLConnection
3737

38-
static var dialect: SQLDialect { .mysql }
38+
public static var dialect: SQLDialect { .mysql }
3939

40-
var pools: [String: GroupPool] = [:]
40+
public var pools: [String: GroupPool] = [:]
4141

42-
let logger: Logger
43-
let eventLoopGroup: EventLoopGroup
42+
public let logger: Logger
43+
public let eventLoopGroup: EventLoopGroup
4444

45-
required init (eventLoopGroup: EventLoopGroup, logger: Logger) {
45+
public required init (eventLoopGroup: EventLoopGroup, logger: Logger) {
4646
self.eventLoopGroup = eventLoopGroup
4747
self.logger = logger
4848
}
4949

5050
/// Gives a connection to the database and closes it automatically in both success and error cases
51-
func connection<T>(to db: DatabaseIdentifier,
51+
public func connection<T>(to db: DatabaseIdentifier,
5252
on eventLoop: EventLoop,
5353
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
5454
self.db(db, on: eventLoop).withConnection { closure($0) }
5555
}
5656

57-
func db(_ db: DatabaseIdentifier, on eventLoop: EventLoop) -> MySQLDatabase {
57+
public func db(_ db: DatabaseIdentifier, on eventLoop: EventLoop) -> MySQLDatabase {
5858
_ConnectionPoolMySQLDatabase(pool: pool(db, for: eventLoop), logger: logger, eventLoop: eventLoop)
5959
}
6060

0 commit comments

Comments
 (0)