Skip to content

yggdrasil.mongoengine.lib

lib

get_connection_settings

get_connection_settings(
    alias: str = "default",
    db: str | None = None,
    jsonable: bool = False,
    context_key: str | None = None,
    environ: MutableMapping[str, str] | None = None,
) -> dict[str, Any]

Return registered MongoEngine connection settings for an alias.

When a context key is available, the contextual alias is preferred first. If jsonable is true, the result is reduced to a JSON-safe subset that can be serialized into the environment and restored later.

connect

connect(
    db: str | None = None,
    alias: str = "default",
    host: str | list[str] | None = None,
    context_key: str | None = None,
    environ: MutableMapping[str, str] | None = None,
    **kwargs: Any
) -> MongoClient

Return a MongoClient for the given alias.

Behavior: - Prefer a context-prefixed alias when available. - Reuse an existing open client if host and database match. - Clear stale cached client/database objects when a refresh is required.

register_connection

register_connection(
    alias: str = "default",
    db: str | None = None,
    host: str | list[str] | None = None,
    server_selection_timeout_ms: int = 5000,
    context_key: str | None = None,
    raise_error: bool = False,
    resolver: Callable[[], str] | str | None = None,
    environ: MutableMapping[str, str] | None = None,
    **kwargs: Any
) -> None

Register a MongoEngine connection and persist a JSON-safe copy of its settings.

The serialized configuration is written to an environment variable so another process or later lookup can restore the registration lazily.

get_connection

get_connection(
    alias: str = "default",
    reconnect: bool = False,
    context_key: str | None = None,
    raise_error: bool = True,
    environ: MutableMapping[str, str] | None = None,
) -> MongoClient | None

Return a MongoClient for an alias.

Lookup order: 1. already-registered contextual alias 2. already-registered plain alias 3. env-restored contextual alias 4. env-restored plain alias

get_db

get_db(
    alias: str = "default",
    reconnect: bool = False,
    context_key: str | None = None,
    raise_error: bool = True,
    environ: MutableMapping[str, str] | None = None,
) -> Database | None

Return a PyMongo Database for an alias.

This wrapper mirrors the custom connection resolution logic and refreshes MongoEngine's cached database object when reconnecting.