Instance Method
get(fromDB:uri:queryItems:dateDecodingStrategy:eventLoopGroup:)
Retrieves and decodes a document of a specified type from a database on the CouchDB server.
func get<T>(fromDB dbName: String, uri: String, queryItems: [URLQueryItem]? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .secondsSince1970, eventLoopGroup: (any EventLoopGroup)? = nil) async throws -> T where T : CouchDBRepresentable
Parameters
- dbName
The name of the database from which to fetch the document.
- uri
The URI path of the specific document within the database (e.g., a document ID).
- queryItems
An optional array of URLQueryItem to specify query parameters for the request.
- dateDecodingStrategy
The date decoding strategy to use when decoding dates. Defaults to .secondsSince1970.
- eventLoopGroup
An optional EventLoopGroup for executing network operations. If not provided, the function uses a shared HTTPClient.
Return Value
A document of type T, where T conforms to CouchDBRepresentable.
Discussion
This generic method fetches a document from a specific database resource and decodes the buffered response body into the requested CouchDBRepresentable type. It supports custom query parameters, a configurable date decoding strategy, and an optional custom EventLoopGroup.
Throws
A CouchDBClientError if the resource is not found, authentication fails, the response body is missing, or CouchDB returns an error payload that maps to .getError(error:). Non-CouchDB decoding failures are propagated as the underlying decoding error.
Example Usage:
Define Your Document Model:
struct MyDocumentType: CouchDBRepresentable {
var name: String
var _id: String = UUID().uuidString
var _rev: String?
func updateRevision(_ newRevision: String) -> Self {
return MyDocumentType(name: name, _id: _id, _rev: newRevision)
}
}
Retrieve a Document by ID:
let doc: MyDocumentType = try await couchDBClient.get(
fromDB: "myDatabase",
uri: "documentID"
)
print(doc)
Note
Ensure that the CouchDB server is running and accessible before calling this function. Handle thrown errors appropriately, especially for authentication failures and data decoding issues.