-
Notifications
You must be signed in to change notification settings - Fork 67
SWIFT-639 Update docs examples #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3373fde
wip
kmahar 26408ca
update async examples
kmahar ac9d053
format/lint
kmahar 052145a
address comments
kmahar 4034bbb
add SwiftNIO dependency explicitly
kmahar cb4ec3d
format
kmahar 6fdbfea
fix NIO dependency, get rid of futureChangeStream variables
kmahar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| // swift-tools-version:4.2 | ||
| // swift-tools-version:5.0 | ||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "DocsExamples", | ||
| dependencies: [ | ||
| .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")) | ||
| .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")), | ||
| .package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.0.0")) | ||
| ], | ||
| targets: [ | ||
| .target(name: "DocsExamples", dependencies: ["MongoSwift"]) | ||
| .target(name: "SyncExamples", dependencies: ["MongoSwiftSync"]), | ||
| .target(name: "AsyncExamples", dependencies: ["MongoSwift", "NIO"]) | ||
| ] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import Foundation | ||
| import MongoSwift | ||
| import NIO | ||
|
|
||
| // swiftlint:disable force_unwrapping | ||
|
|
||
| /// Examples used for the MongoDB documentation on Causal Consistency. | ||
| /// - SeeAlso: https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#examples | ||
| private func causalConsistency() throws { | ||
| let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
| let client1 = try MongoClient(using: elg) | ||
| defer { | ||
| client1.syncShutdown() | ||
| try? elg.syncShutdownGracefully() | ||
| } | ||
|
|
||
| // Start Causal Consistency Example 1 | ||
| let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true)) | ||
| let currentDate = Date() | ||
| var dbOptions = DatabaseOptions( | ||
| readConcern: ReadConcern(.majority), | ||
| writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000) | ||
| ) | ||
| let items = client1.db("test", options: dbOptions).collection("items") | ||
| let result1 = items.updateOne( | ||
| filter: ["sku": "111", "end": .null], | ||
| update: ["$set": ["end": .datetime(currentDate)]], | ||
| session: s1 | ||
| ).flatMap { _ in | ||
| items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1) | ||
| } | ||
| // End Causal Consistency Example 1 | ||
|
|
||
| let client2 = try MongoClient(using: elg) | ||
|
|
||
| // Start Causal Consistency Example 2 | ||
| let options = ClientSessionOptions(causalConsistency: true) | ||
| let result2: EventLoopFuture<Void> = client2.withSession(options: options) { s2 in | ||
| // The cluster and operation times are guaranteed to be non-nil since we already used s1 for operations above. | ||
| s2.advanceClusterTime(to: s1.clusterTime!) | ||
| s2.advanceOperationTime(to: s1.operationTime!) | ||
|
|
||
| dbOptions.readPreference = ReadPreference(.secondary) | ||
| let items2 = client2.db("test", options: dbOptions).collection("items") | ||
|
|
||
| return items2.find(["end": .null], session: s2).flatMap { cursor in | ||
| cursor.forEach { item in | ||
| print(item) | ||
| } | ||
| } | ||
| } | ||
| // End Causal Consistency Example 2 | ||
| } | ||
|
|
||
| /// Examples used for the MongoDB documentation on Change Streams. | ||
| /// - SeeAlso: https://docs.mongodb.com/manual/changeStreams/ | ||
| private func changeStreams() throws { | ||
| let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
| let client = try MongoClient(using: elg) | ||
| let db = client.db("example") | ||
|
|
||
| // The following examples assume that you have connected to a MongoDB replica set and have | ||
| // accessed a database that contains an inventory collection. | ||
|
|
||
| do { | ||
| // Start Changestream Example 1 | ||
| let inventory = db.collection("inventory") | ||
|
|
||
| // Option 1: retrieve next document via next() | ||
| let next = inventory.watch().flatMap { cursor in | ||
| cursor.next() | ||
| } | ||
|
|
||
| // Option 2: register a callback to execute for each document | ||
| let result = inventory.watch().flatMap { cursor in | ||
| cursor.forEach { event in | ||
| // process event | ||
| print(event) | ||
| } | ||
| } | ||
| // End Changestream Example 1 | ||
| } | ||
|
|
||
| do { | ||
| // Start Changestream Example 2 | ||
| let inventory = db.collection("inventory") | ||
|
|
||
| // Option 1: use next() to iterate | ||
| let next = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup)) | ||
| .flatMap { changeStream in | ||
| changeStream.next() | ||
| } | ||
|
|
||
| // Option 2: register a callback to execute for each document | ||
| let result = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup)) | ||
| .flatMap { changeStream in | ||
| changeStream.forEach { event in | ||
| // process event | ||
| print(event) | ||
| } | ||
| } | ||
| // End Changestream Example 2 | ||
| } | ||
|
|
||
| do { | ||
patrickfreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Start Changestream Example 3 | ||
| let inventory = db.collection("inventory") | ||
|
|
||
| inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup)) | ||
| .flatMap { changeStream in | ||
| changeStream.next().map { _ in | ||
| changeStream.resumeToken | ||
| }.always { _ in | ||
| _ = changeStream.kill() | ||
| } | ||
| }.flatMap { resumeToken in | ||
| inventory.watch(options: ChangeStreamOptions(resumeAfter: resumeToken)).flatMap { newStream in | ||
| newStream.forEach { event in | ||
| // process event | ||
| print(event) | ||
| } | ||
| } | ||
| } | ||
| // End Changestream Example 3 | ||
| } | ||
|
|
||
| do { | ||
| // Start Changestream Example 4 | ||
| let pipeline: [Document] = [ | ||
| ["$match": ["fullDocument.username": "alice"]], | ||
| ["$addFields": ["newField": "this is an added field!"]] | ||
| ] | ||
| let inventory = db.collection("inventory") | ||
|
|
||
| // Option 1: use next() to iterate | ||
| let next = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in | ||
| changeStream.next() | ||
| } | ||
|
|
||
| // Option 2: register a callback to execute for each document | ||
| let result = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in | ||
| changeStream.forEach { event in | ||
| // process event | ||
| print(event) | ||
| } | ||
| } | ||
| // End Changestream Example 4 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.