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.

It is only recommended to utilize this extension if you are using Vapor’s EventLoopFuture APIs. If you are using Vapor’s async/await APIs, please use the global client accessible via Application.mongoDB.

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)
     }
 }