Skip to content

Commit 6cc0a8e

Browse files
committed
add typealias, make types encodable, use collectiontype in find and modify ops
1 parent e020cb5 commit 6cc0a8e

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

Sources/MongoSwift/MongoCollection+BulkWrite.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ extension MongoCollection {
99
* - requests: a `[WriteModel]` containing the writes to perform
1010
* - options: optional `BulkWriteOptions` to use while executing the operation
1111
*
12-
* - Returns: a `BulkWriteResult`
12+
* - Returns: a `BulkWriteResult`, or `nil` if the write concern is unacknowledged.
1313
*
1414
* - Throws:
1515
* - `MongoError.invalidArgument` if `requests` is empty
1616
* - `MongoError.commandError` if any error occurs while performing the writes
1717
*/
1818
@discardableResult
19-
public func bulkWrite(_ requests: [WriteModel], options: BulkWriteOptions? = nil) throws -> BulkWriteResult {
19+
public func bulkWrite(_ requests: [WriteModel], options: BulkWriteOptions? = nil) throws -> BulkWriteResult? {
2020
throw MongoError.commandError(message: "Unimplemented command")
2121
}
2222
}
2323

2424
/// Options to use when performing a bulk write operation on a `MongoCollection`.
25-
public struct BulkWriteOptions {
25+
public struct BulkWriteOptions: Encodable {
2626
/// If `true` (the default), when an insert fails, return without performing the remaining writes.
2727
/// If `false`, when a write fails, continue with the remaining writes, if any.
2828
public var ordered: Bool = true
@@ -32,7 +32,7 @@ public struct BulkWriteOptions {
3232
}
3333

3434
/// A protocol indicating write types that can be batched together using `MongoCollection.bulkWrite`.
35-
public protocol WriteModel {}
35+
public protocol WriteModel: Encodable {}
3636

3737
/// A model for an `insertOne` command.
3838
public struct InsertOneModel: WriteModel {

Sources/MongoSwift/MongoCollection+FindAndModify.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ extension MongoCollection {
99
* - filter: `Document` representing the match criteria
1010
* - options: Optional `FindOneAndDeleteOptions` to use when executing the command
1111
*
12-
* - Returns: The deleted document, or `nil` if no document was deleted.
12+
* - Returns: The deleted document, represented as a `CollectionType`, or `nil` if no document was deleted.
1313
*/
1414
@discardableResult
15-
public func findOneAndDelete(_ filter: Document, options: FindOneAndDeleteOptions? = nil) throws -> Document? {
15+
public func findOneAndDelete(_ filter: Document, options: FindOneAndDeleteOptions? = nil) throws -> CollectionType? {
1616
throw MongoError.commandError(message: "Unimplemented command")
1717
}
1818

@@ -21,15 +21,15 @@ extension MongoCollection {
2121
*
2222
* - Parameters:
2323
* - filter: `Document` representing the match criteria
24-
* - replacement: a `Document` to replace the found one
24+
* - replacement: a `CollectionType` to replace the found document
2525
* - options: Optional `FindOneAndReplaceOptions` to use when executing the command
2626
*
27-
* - Returns: Either the original document or its replacement depending on selected options,
28-
* or `nil` if there was no match.
27+
* - Returns: A `CollectionType`, representing either the original document or its replacement,
28+
* depending on selected options, or `nil` if there was no match.
2929
*/
3030
@discardableResult
31-
public func findOneAndReplace(filter: Document, replacement: Document,
32-
options: FindOneAndDeleteOptions? = nil) throws -> Document? {
31+
public func findOneAndReplace(filter: Document, replacement: T,
32+
options: FindOneAndDeleteOptions? = nil) throws -> CollectionType? {
3333
throw MongoError.commandError(message: "Unimplemented command")
3434
}
3535

@@ -38,7 +38,7 @@ extension MongoCollection {
3838
*
3939
* - Parameters:
4040
* - filter: `Document` representing the match criteria
41-
* - replacement: a `Document` to replace the found one
41+
* - update: a `Document` containing updates to apply
4242
* - options: Optional `FindOneAndUpdateOptions` to use when executing the command
4343
*
4444
* - Returns: Either the original or updated document depending on selected options,
@@ -52,16 +52,20 @@ extension MongoCollection {
5252
}
5353

5454
/// Indicates which document to return in a find and modify operation.
55-
public enum ReturnDocument {
55+
public enum ReturnDocument: Encodable {
5656
/// Indicates to return the document before the update, replacement, or insert occured.
5757
case before
5858

5959
/// Indicates to return the document after the update, replacement, or insert occured.
6060
case after
61+
62+
public func encode(to encoder: Encoder) throws {
63+
// fill in later on
64+
}
6165
}
6266

6367
/// Options to use when executing a `findOneAndDelete` command on a `MongoCollection`.
64-
public struct FindOneAndDeleteOptions {
68+
public struct FindOneAndDeleteOptions: Encodable {
6569
/// Specifies a collation to use.
6670
public let collation: Document?
6771

@@ -73,10 +77,13 @@ public struct FindOneAndDeleteOptions {
7377

7478
/// Determines which document the operation modifies if the query selects multiple documents.
7579
public let sort: Document?
80+
81+
/// An optional `WriteConcern` to use for the command.
82+
public let writeConcern: WriteConcern?
7683
}
7784

7885
/// Options to use when executing a `findOneAndReplace` command on a `MongoCollection`.
79-
public struct FindOneAndReplaceOptions {
86+
public struct FindOneAndReplaceOptions: Encodable {
8087
/// If `true`, allows the write to opt-out of document level validation.
8188
public let bypassDocumentValidation: Bool?
8289

@@ -97,10 +104,13 @@ public struct FindOneAndReplaceOptions {
97104

98105
/// When `true`, creates a new document if no document matches the query.
99106
public let upsert: Bool?
107+
108+
/// An optional `WriteConcern` to use for the command.
109+
public let writeConcern: WriteConcern?
100110
}
101111

102112
/// Options to use when executing a `findOneAndUpdate` command on a `MongoCollection`.
103-
public struct FindOneAndUpdateOptions {
113+
public struct FindOneAndUpdateOptions: Encodable {
104114
/// A set of filters specifying to which array elements an update should apply.
105115
public let arrayFilters: [Document]?
106116

@@ -124,4 +134,7 @@ public struct FindOneAndUpdateOptions {
124134

125135
/// When `true`, creates a new document if no document matches the query.
126136
public let upsert: Bool?
137+
138+
/// An optional `WriteConcern` to use for the command.
139+
public let writeConcern: WriteConcern?
127140
}

Sources/MongoSwift/MongoCollection+Read.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ extension MongoCollection {
7272
*
7373
* - Parameters:
7474
* - filter: a `Document`, the filter that documents must match in order to be counted
75-
* - options: Optional `CountOptions` to use when executing the command
75+
* - options: Optional `CountDocumentsOptions` to use when executing the command
7676
*
7777
* - Returns: The count of the documents that matched the filter
7878
*/
79-
public func countDocuments(_ filter: Document = [:], options: CountOptions? = nil) throws -> Int {
79+
public func countDocuments(_ filter: Document = [:], options: CountDocumentsOptions? = nil) throws -> Int {
8080
// TODO SWIFT-133: implement this https://jira.mongodb.org/browse/SWIFT-133
8181
throw MongoError.commandError(message: "Unimplemented command")
8282
}
@@ -243,6 +243,9 @@ public struct CountOptions: Encodable {
243243
}
244244
}
245245

246+
/// The `countDocuments` command takes the same options as the deprecated `count`.
247+
public typealias CountDocumentsOptions = CountOptions
248+
246249
/// Options to use when executing an `estimatedDocumentCount` command on a `MongoCollection`.
247250
public struct EstimatedDocumentCountOptions {
248251
/// The maximum amount of time to allow the query to run.

0 commit comments

Comments
 (0)