-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SwifQL/implement-conveniences
Table: implement convenience `all` and `first`
- Loading branch information
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters