Database
The Database
module provides functionality for connecting to and interacting with MongoDB databases in RxInferServer.jl. This module uses the Mongoc.jl package to provide MongoDB integration. It implements a scoped connection pattern that ensures proper resource cleanup and provides convenient access to database resources.
Configuration
Read more about the configuration of the database in the Database Configuration section.
SSL Certificates
When connecting to MongoDB servers, it is often require to provide SSL certificates. RxInferServer will automatically try to find the certificates on your system and append them to the MongoDB connection string when MongoDB connection string does not contain the "localhost" or "127.0.0.1" address. You can manually set the SSL certificates file by setting the RxInferServer.Database.RXINFER_SERVER_SSL_CA_FILE
environment variable to your own MongoDB connection string. RxInferServer also will not inject the TLS CA file if the MongoDB connection string already contains the tlsCAFile
parameter.
API Reference
RxInferServer.Database.with_connection
— Functionwith_connection(f::F; url::String = RXINFER_SERVER_MONGODB_URL(), database::String = RXINFER_SERVER_MONGODB_DATABASE(), check_connection::Bool = true) where {F}
Establishes a connection to the MongoDB database and executes the given function with the connection and database scoped. This function automatically handles cleanup of MongoDB resources by destroying the client when the provided function completes.
Arguments
f::F
: The function to execute with the database connection.url::String
: The URL of the MongoDB server.database::String
: The name of the database to use.
with_connection(f; url = RXINFER_SERVER_MONGODB_URL(), database = RXINFER_SERVER_MONGODB_DATABASE(), check_connection = true) do
# Your code here
client = Database.client()
database = Database.database()
collection = Database.collection("users")
end
See also RxInferServer.Database.client
, RxInferServer.Database.database
, RxInferServer.Database.collection
.
RxInferServer.Database.client
— Functionclient()::Mongoc.Client
Returns the current database client. Throws an error if called outside of a with_connection
block.
See also RxInferServer.Database.with_connection
, RxInferServer.Database.database
, RxInferServer.Database.collection
.
RxInferServer.Database.database
— Functiondatabase()::Mongoc.Database
Returns the current database. Throws an error if called outside of a with_connection
block.
See also RxInferServer.Database.client
, RxInferServer.Database.with_connection
, RxInferServer.Database.collection
.
RxInferServer.Database.collection
— Functioncollection(name::String)::Mongoc.Collection
Returns the collection with the given name from the current database. Throws an error if called outside of a with_connection
block.
See also RxInferServer.Database.client
, RxInferServer.Database.database
, RxInferServer.Database.with_connection
.
RxInferServer.Database.find_ssl_certificates
— Functionfind_ssl_certificates()::Dict{String, Vector{String}}
Searches for SSL certificates in default locations based on the operating system. Returns a dictionary with keys for different certificate types and values as vectors of found file paths. Certificate locations are prioritized with standard system locations first.
The function searches for:
- CA certificates (trusted root certificates)
- Client certificates (for client authentication)
For Linux systems, it prioritizes locations where certificates are installed via package managers (e.g., apt-get install ca-certificates
).
Returns
Dict{String, Vector{String}}
: Dictionary with keys "cacerts" and "clientcerts"
RxInferServer.Database.inject_tls_ca_file
— Functioninject_tls_ca_file(url::String)::String
Injects the TLS CA file into the MongoDB connection URL if it's not already present. This function adds the tlsCAFile parameter to the URL in the following priority:
- Uses RXINFERSERVERSSLCAFILE environment variable if set
- Automatically finds SSL certificates if the environment variable is empty and the connection is not to localhost
- Leaves the URL unchanged if it already contains a tlsCAFile parameter or points to localhost/127.0.0.1
Arguments
url::String
: The MongoDB connection URL
Returns
String
: The MongoDB connection URL with tlsCAFile parameter added if needed
RxInferServer.Database.RedactedURL
— TypeRedactedURL(url::String)
A type that redacts the URL for logging and display purposes. Removes credentials and sensitive parameters from the URL.
RxInferServer.Database.DatabaseFailedConnectionError
— TypeDatabaseFailedConnectionError(url, cause) <: Exception
An error that occurs when the server fails to connect to the MongoDB database.