Instance Method
find(inDB:query:dateDecodingStrategy:eventLoopGroup:)
Performs a Mango query and decodes the matching documents.
func find<T>(inDB dbName: String, query: MangoQuery, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .secondsSince1970, eventLoopGroup: (any EventLoopGroup)? = nil) async throws -> [T] where T : CouchDBRepresentable
Parameters
- dbName
The name of the database in which to perform the query.
- query
A MangoQuery object that defines the criteria for selecting documents.
- dateDecodingStrategy
The date decoding strategy to use for decoding dates within the documents. Defaults to .secondsSince1970.
- eventLoopGroup
An optional EventLoopGroup for executing network operations. If not provided, the function defaults to using a shared instance of HTTPClient.
Return Value
An array of documents of type T, where T conforms to CouchDBRepresentable.
Discussion
This generic method sends a MangoQuery to CouchDB’s _find endpoint and decodes the resulting documents into the requested CouchDBRepresentable type.
Throws
A CouchDBClientError if authentication fails, the response body is missing, or CouchDB returns an error payload that maps to .findError(error:). Non-CouchDB decoding failures are propagated as the underlying decoding error.
Example Usage:
let query = MangoQuery(selector: ["name": .string("Sam")])
let documents: [MyDocumentType] = try await couchDBClient.find(
inDB: "myDatabase",
query: query
)
print(documents)
Note
Ensure that the CouchDB server is running and accessible before calling this function. Handle thrown errors appropriately, especially for data decoding issues or query mismatches.