Initializer
init(config:httpClient:eventLoopGroup:)
Initializes a new instance of the CouchDB client using the provided configuration.
init(config: CouchDBClient.Config, httpClient: HTTPClient? = nil, eventLoopGroup: (any EventLoopGroup)? = nil)
Parameters
- config
A CouchDBClient.Config instance containing the configuration details, including protocol, host, port, username, and password.
- httpClient
An optional HTTPClient instance. If provided, it is reused for every request and shut down by shutdown().
- eventLoopGroup
An optional EventLoopGroup (e.g. Vapor’s app.eventLoopGroup) that all requests should run on. Used only when httpClient is nil: the client then creates a single long-lived HTTPClient bound to this group and shuts it down in shutdown(). When both httpClient and eventLoopGroup are nil, the globally shared HTTPClient.shared is used (recommended default).
Discussion
This initializer sets up the client with connection parameters and securely handles the user password, supporting environment variable fallback for sensitive data. It allows for optional customization of the connection parameters such as protocol, host, and port.
Example Usage:
// Create a configuration:
let config = CouchDBClient.Config(
couchProtocol: .http,
couchHost: "127.0.0.1",
couchPort: 5984,
userName: "user",
userPassword: "myPassword",
requestsTimeout: 30
)
// Create a client instance:
let couchDBClient = CouchDBClient(config: config)
If you prefer not to include your password in the code, you can pass the COUCHDB_PASS environment variable in your command line. For example:
COUCHDB_PASS=myPassword /path/.build/x86_64-unknown-linux-gnu/release/Run
In this case, you can omit the userPassword parameter in the configuration:
let config = CouchDBClient.Config(
userName: "user"
)
let couchDBClient = CouchDBClient(config: config)
Note
Ensure that the CouchDB server is running and accessible at the specified couchHost and couchPort before attempting to connect.