|
| 1 | +import libmongoc |
| 2 | + |
| 3 | +/// An extension of `MongoCollection` encapsulating find and modify operations. |
| 4 | +extension MongoCollection { |
| 5 | + /** |
| 6 | + * Finds a single document and deletes it, returning the original. |
| 7 | + * |
| 8 | + * - Parameters: |
| 9 | + * - filter: `Document` representing the match criteria |
| 10 | + * - options: Optional `FindOneAndDeleteOptions` to use when executing the command |
| 11 | + * |
| 12 | + * - Returns: The deleted document, represented as a `CollectionType`, or `nil` if no document was deleted. |
| 13 | + * - Throws: |
| 14 | + * - `MongoError.commandError` if there are any errors executing the command. |
| 15 | + * - A `DecodingError` if the deleted document cannot be decoded to a `CollectionType` value |
| 16 | + */ |
| 17 | + @discardableResult |
| 18 | + public func findOneAndDelete(_ filter: Document, options: FindOneAndDeleteOptions? = nil) throws -> CollectionType? { |
| 19 | + throw MongoError.commandError(message: "Unimplemented command") |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Finds a single document and replaces it, returning either the original or the replaced document. |
| 24 | + * |
| 25 | + * - Parameters: |
| 26 | + * - filter: `Document` representing the match criteria |
| 27 | + * - replacement: a `CollectionType` to replace the found document |
| 28 | + * - options: Optional `FindOneAndReplaceOptions` to use when executing the command |
| 29 | + * |
| 30 | + * - Returns: A `CollectionType`, representing either the original document or its replacement, |
| 31 | + * depending on selected options, or `nil` if there was no match. |
| 32 | + * - Throws: |
| 33 | + * - `MongoError.commandError` if there are any errors executing the command. |
| 34 | + * - An `EncodingError` if `replacement` cannot be encoded to a `Document` |
| 35 | + * - A `DecodingError` if the replaced document cannot be decoded to a `CollectionType` value |
| 36 | + */ |
| 37 | + @discardableResult |
| 38 | + public func findOneAndReplace(filter: Document, replacement: CollectionType, |
| 39 | + options: FindOneAndDeleteOptions? = nil) throws -> CollectionType? { |
| 40 | + throw MongoError.commandError(message: "Unimplemented command") |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Finds a single document and updates it, returning either the original or the updated document. |
| 45 | + * |
| 46 | + * - Parameters: |
| 47 | + * - filter: `Document` representing the match criteria |
| 48 | + * - update: a `Document` containing updates to apply |
| 49 | + * - options: Optional `FindOneAndUpdateOptions` to use when executing the command |
| 50 | + * |
| 51 | + * - Returns: A `CollectionType` representing either the original or updated document, |
| 52 | + * depending on selected options, or `nil` if there was no match. |
| 53 | + * - Throws: |
| 54 | + * - `MongoError.commandError` if there are any errors executing the command. |
| 55 | + * - A `DecodingError` if the updated document cannot be decoded to a `CollectionType` value |
| 56 | + */ |
| 57 | + @discardableResult |
| 58 | + public func findOneAndUpdate(filter: Document, update: Document, |
| 59 | + options: FindOneAndUpdateOptions? = nil) throws -> CollectionType? { |
| 60 | + throw MongoError.commandError(message: "Unimplemented command") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Indicates which document to return in a find and modify operation. |
| 65 | +public enum ReturnDocument: Encodable { |
| 66 | + /// Indicates to return the document before the update, replacement, or insert occured. |
| 67 | + case before |
| 68 | + |
| 69 | + /// Indicates to return the document after the update, replacement, or insert occured. |
| 70 | + case after |
| 71 | + |
| 72 | + public func encode(to encoder: Encoder) throws { |
| 73 | + // fill in later on |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Options to use when executing a `findOneAndDelete` command on a `MongoCollection`. |
| 78 | +public struct FindOneAndDeleteOptions: Encodable { |
| 79 | + /// Specifies a collation to use. |
| 80 | + public let collation: Document? |
| 81 | + |
| 82 | + /// The maximum amount of time to allow the query to run. |
| 83 | + public let maxTimeMS: Int64? |
| 84 | + |
| 85 | + /// Limits the fields to return for the matching document. |
| 86 | + public let projection: Document? |
| 87 | + |
| 88 | + /// Determines which document the operation modifies if the query selects multiple documents. |
| 89 | + public let sort: Document? |
| 90 | + |
| 91 | + /// An optional `WriteConcern` to use for the command. |
| 92 | + public let writeConcern: WriteConcern? |
| 93 | +} |
| 94 | + |
| 95 | +/// Options to use when executing a `findOneAndReplace` command on a `MongoCollection`. |
| 96 | +public struct FindOneAndReplaceOptions: Encodable { |
| 97 | + /// If `true`, allows the write to opt-out of document level validation. |
| 98 | + public let bypassDocumentValidation: Bool? |
| 99 | + |
| 100 | + /// Specifies a collation to use. |
| 101 | + public let collation: Document? |
| 102 | + |
| 103 | + /// The maximum amount of time to allow the query to run. |
| 104 | + public let maxTimeMS: Int64? |
| 105 | + |
| 106 | + /// Limits the fields to return for the matching document. |
| 107 | + public let projection: Document? |
| 108 | + |
| 109 | + /// When `ReturnDocument.After`, returns the replaced or inserted document rather than the original. |
| 110 | + public let returnDocument: ReturnDocument? |
| 111 | + |
| 112 | + /// Determines which document the operation modifies if the query selects multiple documents. |
| 113 | + public let sort: Document? |
| 114 | + |
| 115 | + /// When `true`, creates a new document if no document matches the query. |
| 116 | + public let upsert: Bool? |
| 117 | + |
| 118 | + /// An optional `WriteConcern` to use for the command. |
| 119 | + public let writeConcern: WriteConcern? |
| 120 | +} |
| 121 | + |
| 122 | +/// Options to use when executing a `findOneAndUpdate` command on a `MongoCollection`. |
| 123 | +public struct FindOneAndUpdateOptions: Encodable { |
| 124 | + /// A set of filters specifying to which array elements an update should apply. |
| 125 | + public let arrayFilters: [Document]? |
| 126 | + |
| 127 | + /// If `true`, allows the write to opt-out of document level validation. |
| 128 | + public let bypassDocumentValidation: Bool? |
| 129 | + |
| 130 | + /// Specifies a collation to use. |
| 131 | + public let collation: Document? |
| 132 | + |
| 133 | + /// The maximum amount of time to allow the query to run. |
| 134 | + public let maxTimeMS: Int64? |
| 135 | + |
| 136 | + /// Limits the fields to return for the matching document. |
| 137 | + public let projection: Document? |
| 138 | + |
| 139 | + /// When`ReturnDocument.After`, returns the updated or inserted document rather than the original. |
| 140 | + public let returnDocument: ReturnDocument? |
| 141 | + |
| 142 | + /// Determines which document the operation modifies if the query selects multiple documents. |
| 143 | + public let sort: Document? |
| 144 | + |
| 145 | + /// When `true`, creates a new document if no document matches the query. |
| 146 | + public let upsert: Bool? |
| 147 | + |
| 148 | + /// An optional `WriteConcern` to use for the command. |
| 149 | + public let writeConcern: WriteConcern? |
| 150 | +} |
0 commit comments