Hi Stephane and ReactiveMongo users,
I've been working on a project that combines Twitter's Finagle and ReactiveMongo Driver. I managed to solve two interesting issues: (a) converting Scala Future to Twitter's Future and (b) an implicit mongo connection for a whole project. I decided to put my findings here. Hope it will help someone.
The 'FutureConverter' class might be used as follows:
val s = new Service[Req, Rep] with FutureConverter {
def apply(req: Req): Future[Rep] = {
val f = ... // a Scala Future returned by ReactiveMongo
f.toTwitterFuture
}
}
The documentation says there should a singleton 'MongoConnection' in the system. I found a truly Scala way of handling that. This is especially useful when the functionality is composed by services/modules which are Scala's objects. The usage example looks like this (example of usage with Finagle's Service):
object FooService extends Service[Request, Response]
with ImplicitMongoConnection[Future[Response]] {
// we have to explicitly import implicit value into the scope
// we can do it at 'file' scope level
import Main._
def apply(req: Request): Future[Response] = withConnection { implicit connection =>
// do all the magic here with 'connection'
// and pack the result into a future
Future.never
}
}
The question to Stephane. Is it possible to have an ImplicitMongoConnection trait out-of-the-box in further releases. I found this tiny class really useful.