Vapor Extensions
-
An extension to Vapor’s
Applicationtype to add support for configuring your application to interact with a MongoDB deployment. All of the API is namespaced under the.mongoDBproperty on theApplication.MongoDBtype. 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.MongoDBfor further details.Declaration
Swift
extension Application -
An extension to Vapor’s
Requesttype to add support for conveniently accessing MongoDB core types e.g. e.g. clients, databases, and collections which return futures on on the sameEventLoopwhich theRequestis on.This extension provides a
Request.mongoDB.clientproperty which you can use as follows from within aRequesthandler:req.mongoDB.client.db("home").collection("kittens", withType: Kitten.self).insertOne(myKitten)We recommend utilizing this API to add extensions to
Requestfor 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
View on GitHub
Install in Dash
Vapor Extensions Reference