Use Dependencies with Vapor.
Status: Experimental / In Progress
You can define a dependency to require the Application
, Request
or Database
to be created.
extension AnalyzerDependency: ApplicationDependencyKey {
public static func live(app: Application) -> Self { ... }
}
extension SearchDependency: RequestDependencyKey {
static func live(request: Request) -> Self { ... }
}
extension TopicsClient: DatabaseDependencyKey {
public static func live(db: Database) -> Self { ... }
}
// Configure custom dependencies to use
app.dependencies.use(\.analyzer)
app.dependencies.use(\.search)
app.dependencies.use(\.topicsClient)
// Wrap requests in middleware
app.middleware.use(WithDependenciesMiddleware())
// Gain access to configured dependencies during a request.
try await request.yieldDependencies {
// do work, all dependencies are here!
}
// Use outside of request context, for example with Commands.
try await application.withDependencies {
// do work, all dependencies are here!
}
This library is released under the MIT license. See LICENSE for details.