|
| 1 | +#if compiler(>=5.5) && canImport(_Concurrency) && os(Linux) |
| 2 | +/// Extension to `MongoCollection` to support async/await find and modify APIs. |
| 3 | +extension MongoCollection { |
| 4 | + /** |
| 5 | + * Finds a single document and deletes it, returning the original. |
| 6 | + * |
| 7 | + * - Parameters: |
| 8 | + * - filter: a `BSONDocument` representing the match criteria. |
| 9 | + * - options: Optional `FindOneAndDeleteOptions` to use when executing the command. |
| 10 | + * - session: Optional `ClientSession` to use when executing this command. |
| 11 | + * |
| 12 | + * - Returns: The deleted document, represented as a `CollectionType`, or `nil` if no document was deleted. |
| 13 | + * |
| 14 | + * - Throws: |
| 15 | + * - `MongoError.InvalidArgumentError` if any of the provided options are invalid. |
| 16 | + * - `MongoError.LogicError` if the provided session is inactive. |
| 17 | + * - `MongoError.CommandError` if an error occurs that prevents the command from executing. |
| 18 | + * - `MongoError.WriteError` if an error occurs while executing the command. |
| 19 | + * - `DecodingError` if the deleted document cannot be decoded to a `CollectionType` value. |
| 20 | + */ |
| 21 | + @discardableResult |
| 22 | + public func findOneAndDelete( |
| 23 | + _ filter: BSONDocument, |
| 24 | + options: FindOneAndDeleteOptions? = nil, |
| 25 | + session: ClientSession? = nil |
| 26 | + ) async throws -> CollectionType? { |
| 27 | + try await self.findOneAndDelete(filter, options: options, session: session).get() |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Finds a single document and replaces it, returning either the original or the replaced document. |
| 32 | + * |
| 33 | + * - Parameters: |
| 34 | + * - filter: a `BSONDocument` representing the match criteria. |
| 35 | + * - replacement: a `CollectionType` to replace the found document. |
| 36 | + * - options: Optional `FindOneAndReplaceOptions` to use when executing the command. |
| 37 | + * - session: Optional `ClientSession` to use when executing this command. |
| 38 | + * |
| 39 | + * - Returns: A `CollectionType`, representing either the original document or its replacement, |
| 40 | + * depending on selected options, or `nil` if there was no match. |
| 41 | + * |
| 42 | + * - Throws: |
| 43 | + * - `MongoError.InvalidArgumentError` if any of the provided options are invalid. |
| 44 | + * - `MongoError.LogicError` if the provided session is inactive. |
| 45 | + * - `MongoError.CommandError` if an error occurs that prevents the command from executing. |
| 46 | + * - `MongoError.WriteError` if an error occurs while executing the command. |
| 47 | + * - `DecodingError` if the replaced document cannot be decoded to a `CollectionType` value. |
| 48 | + * - `EncodingError` if `replacement` cannot be encoded to a `BSONDocument`. |
| 49 | + */ |
| 50 | + @discardableResult |
| 51 | + public func findOneAndReplace( |
| 52 | + filter: BSONDocument, |
| 53 | + replacement: CollectionType, |
| 54 | + options: FindOneAndReplaceOptions? = nil, |
| 55 | + session: ClientSession? = nil |
| 56 | + ) async throws -> CollectionType? { |
| 57 | + try await self.findOneAndReplace( |
| 58 | + filter: filter, |
| 59 | + replacement: replacement, |
| 60 | + options: options, |
| 61 | + session: session |
| 62 | + ) |
| 63 | + .get() |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Finds a single document and updates it, returning either the original or the updated document. |
| 68 | + * |
| 69 | + * - Parameters: |
| 70 | + * - filter: a `BSONDocument` representing the match criteria. |
| 71 | + * - update: a `BSONDocument` containing updates to apply. |
| 72 | + * - options: Optional `FindOneAndUpdateOptions` to use when executing the command. |
| 73 | + * - session: Optional `ClientSession` to use when executing this command. |
| 74 | + * |
| 75 | + * - Returns: A `CollectionType` representing either the original or updated document, |
| 76 | + * depending on selected options, or `nil` if there was no match. |
| 77 | + * |
| 78 | + * - Throws: |
| 79 | + * - `MongoError.InvalidArgumentError` if any of the provided options are invalid. |
| 80 | + * - `MongoError.LogicError` if the provided session is inactive. |
| 81 | + * - `MongoError.CommandError` if an error occurs that prevents the command from executing. |
| 82 | + * - `MongoError.WriteError` if an error occurs while executing the command. |
| 83 | + * - `DecodingError` if the updated document cannot be decoded to a `CollectionType` value. |
| 84 | + */ |
| 85 | + @discardableResult |
| 86 | + public func findOneAndUpdate( |
| 87 | + filter: BSONDocument, |
| 88 | + update: BSONDocument, |
| 89 | + options: FindOneAndUpdateOptions? = nil, |
| 90 | + session: ClientSession? = nil |
| 91 | + ) async throws -> CollectionType? { |
| 92 | + try await self.findOneAndUpdate( |
| 93 | + filter: filter, |
| 94 | + update: update, |
| 95 | + options: options, |
| 96 | + session: session |
| 97 | + ) |
| 98 | + .get() |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Finds a single document and updates it, returning either the original or the updated document. |
| 103 | + * |
| 104 | + * - Parameters: |
| 105 | + * - filter: a `BSONDocument` representing the match criteria. |
| 106 | + * - pipeline: an array of `BSONDocument` containing the aggregation pipeline to apply. |
| 107 | + * - options: Optional `FindOneAndUpdateOptions` to use when executing the command. |
| 108 | + * - session: Optional `ClientSession` to use when executing this command. |
| 109 | + * |
| 110 | + * - Returns: A `CollectionType` representing either the original or updated document, |
| 111 | + * depending on selected options, or `nil` if there was no match. |
| 112 | + * |
| 113 | + * - Throws: |
| 114 | + * - `MongoError.InvalidArgumentError` if any of the provided options are invalid. |
| 115 | + * - `MongoError.LogicError` if the provided session is inactive. |
| 116 | + * - `MongoError.CommandError` if an error occurs that prevents the command from executing. |
| 117 | + * - `MongoError.WriteError` if an error occurs while executing the command. |
| 118 | + * - `DecodingError` if the updated document cannot be decoded to a `CollectionType` value. |
| 119 | + */ |
| 120 | + @discardableResult |
| 121 | + public func findOneAndUpdate( |
| 122 | + filter: BSONDocument, |
| 123 | + pipeline: [BSONDocument], |
| 124 | + options: FindOneAndUpdateOptions? = nil, |
| 125 | + session: ClientSession? = nil |
| 126 | + ) async throws -> CollectionType? { |
| 127 | + try await self.findOneAndUpdate( |
| 128 | + filter: filter, |
| 129 | + pipeline: pipeline, |
| 130 | + options: options, |
| 131 | + session: session |
| 132 | + ) |
| 133 | + .get() |
| 134 | + } |
| 135 | +} |
| 136 | +#endif |
0 commit comments