Usage in Server Side Swift with Vapor #23
-
Hey, awesome library! I am trying to figure out whether it is possible to use this library in a Vapor project for server-side-swift. Vapor is built with it's own way of defining dependencies. So, maybe it's just not optimal trying to fuse the two concepts. But, perhaps you have some ideas! One concrete situtation I've encountered is the use of databases with Vapor's public enum DatabaseKey: DependencyKey {
public static var liveValue: any Database {
@Dependency(\.application) var app: Application
return app.databases.database(logger: Logger(label: "database"), on: app.eventLoopGroup.any())!
}
}
extension DependencyValues {
public var database: Database {
get { self[DatabaseKey.self] }
set { self[DatabaseKey.self] = newValue }
}
} The problem with this is that I could ever only get one database connection, as the public enum DatabaseKey: DependencyKey {
public static var liveValue: () -> any Database {
@Dependency(\.application) var app: Application
return {
app.databases.database(logger: Logger(label: "database"), on: app.eventLoopGroup.any())!
}
}
} Also, in other dependencies where I want to use a struct UserRepository {
var findAll: () async throws -> [UUID]
static func live(db: Database) -> Self {
.init {
return [.init()]
}
}
}
enum UserRepositoryKey: DependencyKey {
static var liveValue: () -> UserRepository {
{
@Dependency(\.database) var db: Database
return .live(db: db)
}
}
}
extension DependencyValues {
var userRepository: UserRepository {
get { self[UserRepositoryKey.self]() }
set { self[UserRepositoryKey.self] = { newValue } }
}
} Is there any alternative/better solution than this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @madsodgaard, while we haven't specifically explored using the dependencies library in Vapor (though we plan on it soon), we do think it should work quite well. In general, server frameworks are a great example of “single entry point systems”, which this library excels at. However, we have started using this dependencies library on our site, which doesn't use Vapor but we were able to model a database client dependency just fine. Rather than holding a connection to the database directly in dependencies, we hold a connection pool for the database. Would that help your situation? |
Beta Was this translation helpful? Give feedback.
-
@mbrandonw the situation has evolved slightly vapor/vapor#3086 |
Beta Was this translation helpful? Give feedback.
Hi @madsodgaard, while we haven't specifically explored using the dependencies library in Vapor (though we plan on it soon), we do think it should work quite well. In general, server frameworks are a great example of “single entry point systems”, which this library excels at.
However, we have started using this dependencies library on our site, which doesn't use Vapor but we were able to model a database client dependency just fine. Rather than holding a connection to the database directly in dependencies, we hold a connection pool for the database. Would that help your situation?