Vapor Extensions

  • An extension to Vapor’s Application type to add support for configuring your application to interact with a MongoDB deployment. All of the API is namespaced under the .mongoDB property on the Application.MongoDB type. This extension supports the following:

    See Application.MongoDB for further details.

    See more

    Declaration

    Swift

    extension Application
  • 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)
         }
     }
    
    See more

    Declaration

    Swift

    extension Request