Skip to content

Commit

Permalink
Merge pull request #7 from SwifQL/implement-conveniences
Browse files Browse the repository at this point in the history
Table: implement convenience `all` and `first`
  • Loading branch information
MihaelIsaev authored May 8, 2020
2 parents fa2e260 + fc4dd8b commit 3d5958e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
32 changes: 32 additions & 0 deletions Sources/Bridges/Extensions/Table+Conveniences.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Table+Conveniences.swift
// Bridges
//
// Created by Mihael Isaev on 09.05.2020.
//

import Logging
import NIO
import SwifQL

extension Table {
private static func error(_ logger: Logger) {
logger.error(.init(stringLiteral: "Query doesn't work with non-generic database identifier. Please initialize AnyDatabaseIdentifier as MySQL or Postgres explicitly."))
}

public static func all(on db: DatabaseIdentifier, on: AnyBridgesObject) -> EventLoopFuture<[Self]> {
guard let db = db as? AnyDatabaseIdentifiable else {
error(on.logger)
return on.eventLoop.makeFailedFuture(BridgesError.nonGenericDatabaseIdentifier)
}
return db.all(Self.self, on: on)
}

public static func first(on db: DatabaseIdentifier, on: AnyBridgesObject) -> EventLoopFuture<Self?> {
guard let db = db as? AnyDatabaseIdentifiable else {
error(on.logger)
return on.eventLoop.makeFailedFuture(BridgesError.nonGenericDatabaseIdentifier)
}
return db.first(Self.self, on: on)
}
}
5 changes: 4 additions & 1 deletion Sources/Bridges/Protocols/AnyDatabaseIdentifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import SwifQL
import NIO

public protocol AnyDatabaseIdentifiable {}
public protocol AnyDatabaseIdentifiable {
func all<T>(_ table: T.Type, on bridges: AnyBridgesObject) -> EventLoopFuture<[T]> where T: Table
func first<T>(_ table: T.Type, on bridges: AnyBridgesObject) -> EventLoopFuture<T?> where T: Table
}
public protocol AnyMySQLDatabaseIdentifiable: AnyDatabaseIdentifiable {}
public protocol AnyPostgresDatabaseIdentifiable: AnyDatabaseIdentifiable {}

Expand Down

0 comments on commit 3d5958e

Please sign in to comment.