Request

extension Request

An extension to Vapor’s Request type to add support for conveniently accessing MongoDB core types e.g. e.g. clients, databases, and collections which return futures on on the same EventLoop which the Request is on.

This extension provides a Request.mongoDB.client property which you can use as follows from within a Request handler:

 req.mongoDB.client.db("home").collection("kittens", withType: Kitten.self).insertOne(myKitten)

We recommend utilizing this API to add extensions to Request for MongoDB databases and collections you frequently access, for example:

 extension Request {
     /// A collection with an associated `Codable` type `Kitten`.
     var kittenCollection: MongoCollection<Kitten> {
         self.mongoDB.client.db("home").collection("kittens", withType: Kitten.self)
     }
 }