Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Examples/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
disabled_rules:
- explicit_acl
- nesting
10 changes: 7 additions & 3 deletions Examples/VaporExample/Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// swift-tools-version:4.2
// swift-tools-version:5.0
import PackageDescription

let package = Package(
name: "VaporExample",
platforms: [
.macOS(.v10_14)
],
dependencies: [
.package(url: "https://github.com/vapor/vapor", .upToNextMajor(from: "3.3.0")),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
// The driver depends on SwiftNIO 2 and therefore is only compatible with Vapor 4.
.package(url: "https://github.com/vapor/vapor", .exact("4.0.0-beta.3.24")),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(name: "VaporExample", dependencies: ["Vapor", "MongoSwift"])
Expand Down
2 changes: 2 additions & 0 deletions Examples/VaporExample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This is a minimal working example of using the driver in a Vapor application.

**Note**: Since the driver depends on SwiftNIO 2 as of the 1.0.0-rc0 release, it is only compatible with Vapor 4.

To test it out, do the following:
1. Run `mongod` to start MongoDB running on `localhost:27017`.
1. Navigate to the `Examples/` directory (one level up from this one.)
Expand Down
25 changes: 25 additions & 0 deletions Examples/VaporExample/Sources/VaporExample/configure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import MongoSwift
import Vapor

extension Application {
/// A global `MongoClient` for use throughout the application.
var mongoClient: MongoClient {
get {
return self.storage[MongoClientKey.self]!
}
set {
self.storage[MongoClientKey.self] = newValue
}
}

private struct MongoClientKey: StorageKey {
typealias Value = MongoClient
}
}

func configure(_ app: Application) throws {
// Initialize a client using the application's EventLoopGroup.
let client = try MongoClient(using: app.eventLoopGroup)
app.mongoClient = client
try routes(app)
}
27 changes: 9 additions & 18 deletions Examples/VaporExample/Sources/VaporExample/main.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import MongoSwift
import Vapor

/// A Codable type that matches the data in our home.kittens collection.
private struct Kitten: Content {
var name: String
var color: String
}

private let app = try Application()
private let router = try app.make(Router.self)
var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

/// A single collection with type `Kitten`. This allows us to directly retrieve instances of
/// `Kitten` from the collection. `MongoCollection` is safe to share across threads.
private let collection = try MongoClient().db("home").collection("kittens", withType: Kitten.self)
let app = Application(env)

router.get("kittens") { _ -> [Kitten] in
let cursor = try collection.find()
let results = Array(cursor)
if let error = cursor.error {
throw error
}
return results
defer {
// shut down the client and clean up the driver's global resources.
app.mongoClient.syncShutdown()
cleanupMongoSwift()
app.shutdown()
}

try configure(app)
try app.run()
20 changes: 20 additions & 0 deletions Examples/VaporExample/Sources/VaporExample/routes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import MongoSwift
import Vapor

/// A Codable type that matches the data in our home.kittens collection.
struct Kitten: Content {
var name: String
var color: String
}

func routes(_ app: Application) throws {
/// A collection with type `Kitten`. This allows us to directly retrieve instances of
/// `Kitten` from the collection. `MongoCollection` is safe to share across threads.
let collection = app.mongoClient.db("home").collection("kittens", withType: Kitten.self)

app.get("kittens") { _ -> EventLoopFuture<[Kitten]> in
collection.find().flatMap { cursor in
cursor.toArray()
}
}
}