Skip to content

Commit

Permalink
Add support for async/await (#732)
Browse files Browse the repository at this point in the history
* Point to async/await dependencies

* Add async pagination function

* Add async APIs dfor sessions

* Add async sessions

* Add async versions of migrate and revert

* Fix warning for Page Content conformance

* Fix redundant conformance

* Depend on FluentKit release

* AsyncResponseEncodable is available everywhere now

* Use release of Vapor
  • Loading branch information
0xTim authored Oct 26, 2021
1 parent 5810a40 commit ea707ee
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ let package = Package(
.library(name: "Fluent", targets: ["Fluent"]),
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.12.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.39.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.16.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.50.0"),
],
targets: [
.target(name: "Fluent", dependencies: [
Expand Down
24 changes: 24 additions & 0 deletions Sources/Fluent/Concurrency/FluentProvider+Concurrency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if compiler(>=5.5) && canImport(_Concurrency)
import NIOCore
import Vapor

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
extension Application {
/// Automatically runs forward migrations without confirmation.
/// This can be triggered by passing `--auto-migrate` flag.
public func autoMigrate() async throws {
try await self.migrator.setupIfNeeded().flatMap {
self.migrator.prepareBatch()
}.get()
}

/// Automatically runs reverse migrations without confirmation.
/// This can be triggered by passing `--auto-revert` during boot.
public func autoRevert() async throws {
try await self.migrator.setupIfNeeded().flatMap {
self.migrator.revertAllBatches()
}.get()
}
}

#endif
15 changes: 15 additions & 0 deletions Sources/Fluent/Concurrency/Pagination+Concurrency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#if compiler(>=5.5) && canImport(_Concurrency)
import NIOCore
import Vapor

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
extension QueryBuilder {
public func paginate(
for request: Request
) async throws -> Page<Model> {
let page = try request.query.decode(PageRequest.self)
return try await self.paginate(page)
}
}

#endif
28 changes: 28 additions & 0 deletions Sources/Fluent/Concurrency/Sessions+Concurrency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if compiler(>=5.5) && canImport(_Concurrency)
import NIOCore
import Vapor

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
extension Model where Self: SessionAuthenticatable, Self.SessionID == Self.IDValue {
public static func asyncSessionAuthenticator(
_ databaseID: DatabaseID? = nil
) -> AsyncAuthenticator {
AsyncDatabaseSessionAuthenticator<Self>(databaseID: databaseID)
}
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
private struct AsyncDatabaseSessionAuthenticator<User>: AsyncSessionAuthenticator
where User: SessionAuthenticatable, User: Model, User.SessionID == User.IDValue
{
let databaseID: DatabaseID?

func authenticate(sessionID: User.SessionID, for request: Request) async throws {
if let user = try await User.find(sessionID, on: request.db(self.databaseID)) {
request.auth.login(user)
}
}
}

#endif

2 changes: 1 addition & 1 deletion Sources/Fluent/Fluent+Paginate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ extension QueryBuilder {
}
}

extension Page: Content, RequestDecodable, ResponseEncodable where T: Codable { }
extension Page: Content, ResponseEncodable, RequestDecodable, AsyncResponseEncodable, AsyncRequestDecodable where T: Codable { }

0 comments on commit ea707ee

Please sign in to comment.