You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Examples/VaporExample/README.md
+8-13Lines changed: 8 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,22 +8,22 @@ This application is intended to demonstrate best practices for integrating the d
8
8
9
9
The application contains both a REST API as well as a minimal frontend built with Vapor's templating language [Leaf](https://github.com/vapor/leaf).
10
10
11
-
This application require Swift 5.2 and MongoDB 3.6+. It will run on Linux as well as macOS 10.15+.
11
+
This application requires Swift 5.5+ and MongoDB 3.6+. It will run on Linux as well as macOS 12+.
12
+
13
+
If you are looking for an example application that uses older Swift versions prior to the introduction of structured concurrency, please refer to a previous version of this application and README [here](https://github.com/mongodb/mongo-swift-driver/tree/5d9fad121d7cb3ded61087de300ff007766ccd55/Examples/VaporExample).
12
14
13
15
## Building and Running the Application
14
16
1. Install MongoDB on your system if you haven't already. Downloads are available [here](https://www.mongodb.com/download-center/community).
15
17
1. Start up MongoDB running locally: `mongod --dbpath some-directory-here`. You may need to specify a `dbpath` directory for the database to use.
16
18
1. Run `./loadData.sh` to load example application data into the database.
17
-
1. Install Swift 5.2+ on your system if you haven't already. You can download Swift and find instructions for installing it [here](https://swift.org/download/).
19
+
1. Install Swift 5.5+ on your system if you haven't already. You can download Swift and find instructions for installing it [here](https://swift.org/download/).
18
20
1. From the root directory of the project, run `swift build`. This will likely take a while the first time you do so.
19
21
1. Once building has completed, run `swift run` from the root directory. You should get a message that the server has started running on `http://127.0.0.1:8080`.
20
22
1. Open up your browser and visit `http://127.0.0.1:8080`. You should see the application and be able to test out adding, deleting, and editing data in the collection.
21
23
22
24
## Application Architecture
23
25
24
-
This is a fully asynchronous application. At its core is [SwiftNIO](https://github.com/apple/swift-nio), which is used to implement both Vapor and the MongoDB driver.
25
-
26
-
The application has both web and REST API interfaces, which support storing a list of kittens and details about them.
26
+
This is a fully asynchronous application with both web and REST API interfaces, which support storing a list of kittens and details about them.
27
27
28
28
The server will handle the following types of web requests:
29
29
1. A GET request at the root URL `/` loads the main index page containing a list of kittens.
@@ -43,19 +43,14 @@ If you'd like to point the application to a MongoDB server elsewhere (e.g. on [M
43
43
44
44
The call to `configure()` initializes a global `MongoClient` to back your application. `MongoClient` is implemented with that approach in mind: it is safe to use across threads, and is backed by a [connection pool](https://en.wikipedia.org/wiki/Connection_pool) which enables sharing resources throughout the application.
45
45
46
-
Throughout your application, you can access the global client via `app.mongoDB.client`. Note that the global client may return `EventLoopFuture`s on *any*`EventLoop` in the application's `EventLoopGroup`, so if you use this client you will need to ensure you "hop" the futures back to the event loop you are currently on. See the `EventLoopFuture`[documentation](https://apple.github.io/swift-nio/docs/current/NIO/Classes/EventLoopFuture.html) for more details.
47
-
48
-
To avoid the need to hop `EventLoop`s, whenever you are using MongoDB in a request handler, we strongly recommend you use an `EventLoopBoundMongoClient` instead, accessible via `req.mongoDB.client`. This type is a small wrapper around the global client, which returns `EventLoopFuture`s on a specific `EventLoop` which it is "bound" to. Using an `EventLoopBoundMongoClient` that is backed by the same `EventLoop` as a `Request` means you can use the client within a request handler without worrying about thread safety. You can access an `EventLoopBoundMongoClient` for a `Request` via `req.mongoDB.client`.
49
-
50
-
`MongoDatabase`s and `MongoCollection`s you retrieve from an `EventLoopBoundMongoClient` will automatically be bound to the same event loop as the parent client.
46
+
Throughout your application, you can access the global client via `app.mongoDB.client`.
51
47
52
48
For convenience, we recommend adding your own computed properties to `Request` that return `MongoDatabase`s and `MongoCollection`s you frequently access, as is shown in `Sources/App/routes.swift`
53
49
with the `kittenCollection` property:
54
50
```swift
55
51
extensionRequest {
56
-
/// Convenience extension for obtaining a collection which uses the same event loop as a request.
@@ -70,7 +65,7 @@ When creating a `MongoCollection`, you can pass in the name of a `Codable` type:
70
65
let collection = req.mongoDB.client.db("home").collection("kittens", withType: Kitten.self)
71
66
```
72
67
73
-
This will instantiate a `MongoCollection<Kitten>`. You can then use `Kitten` directly with many API methods -- for example, `insertOne` will directly accept a `Kitten` instance, and `findOne` will return an `EventLoopFuture<Kitten>`.
68
+
This will instantiate a `MongoCollection<Kitten>`. You can then use `Kitten` directly with many API methods -- for example, `insertOne` will directly accept a `Kitten` instance, and `findOne` will return a `Kitten?`.
74
69
75
70
Sometimes you may need to work with the `BSONDocument` type as well, for example when providing a query filter. If you want to construct these documents from `Codable` types you may do so using `BSONEncoder`, as we do with the `updateDocument` in the `updateKitten()` method via the `KittenUpdate` struct.
0 commit comments