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_connectionFunction
with_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.

source
RxInferServer.Database.find_ssl_certificatesFunction
find_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"
source
RxInferServer.Database.inject_tls_ca_fileFunction
inject_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:

  1. Uses RXINFERSERVERSSLCAFILE environment variable if set
  2. Automatically finds SSL certificates if the environment variable is empty and the connection is not to localhost
  3. 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
source