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 theApplication.MongoDB
type. This extension supports the following:Configuring a global MongoDB client for your application via
Application.MongoDB.configure(_:options:)
, for example:myApp.mongoDB.configure("mongodb://localhost:27017")
Accessing a global client via
Application.MongoDB.client
, for example:myApp.mongoDB.client.listDatabases()
Cleaning up the global client when your application is shutting down via
Application.MongoDB.cleanup()
, for example:myApp.mongDB.cleanup()
See
See moreApplication.MongoDB
for further details.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 sameEventLoop
which theRequest
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 viaApplication.mongoDB
.This extension provides a
Request.mongoDB.client
property which you can use as follows from within aRequest
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:
See moreextension Request { /// A collection with an associated `Codable` type `Kitten`. var kittenCollection: MongoCollection<Kitten> { self.mongoDB.client.db("home").collection("kittens", withType: Kitten.self) } }
Declaration
Swift
extension Request