Skip to content

Registry

mongo_ops.registry

Summary

Model registration for multi-service initialization.

Classes

ModelRegistry

Registry for managing multiple models and their collections.

This registry allows central management of collections and their associated indexes, making it easier to perform mass initialization at application startup.

Functions
get_cache_backend classmethod
get_cache_backend() -> CacheBackend | None

Get the registered cache backend instance.

Returns:

Type Description
CacheBackend | None

Optional[CacheBackend]: The cache backend, if registered.

get_model classmethod
get_model(collection_name: str) -> type[BaseDocument]

Retrieve a registered model by its collection name.

Parameters:

Name Type Description Default
collection_name str

The name of the collection.

required

Returns:

Type Description
type[BaseDocument]

type[BaseDocument]: The registered model class.

Raises:

Type Description
KeyError

If the model for the given collection is not registered.

initialize_all async classmethod
1
2
3
initialize_all(
    db: AsyncIOMotorDatabase | None = None,
) -> None

Initialize all registered collections and create indexes.

This method should be called during application startup to ensure all necessary indexes exist in the database.

Parameters:

Name Type Description Default
db Optional[AsyncIOMotorDatabase]

Database instance. If not provided, uses the global database from MongoConnectionManager.

None
initialize_cache async classmethod
initialize_cache() -> None

Initialize the registered cache backend.

Must be called after set_cache_backend() and before any cache-backed repository operations. Typically called right after MongoDB connection is established.

Raises:

Type Description
RuntimeError

If no cache backend has been registered.

list_collections classmethod
list_collections() -> list[str]

List all registered collection names.

Returns:

Type Description
list[str]

list[str]: A list of collection names.

register classmethod
1
2
3
4
5
register(
    collection_name: str,
    model: type[BaseDocument],
    indexes: list[tuple] | None = None,
) -> None

Register a model with its collection and indexes.

Parameters:

Name Type Description Default
collection_name str

Name of the MongoDB collection.

required
model type[BaseDocument]

Document model class (subclass of BaseDocument).

required
indexes list[Any]

List of index specifications. Each spec is passed directly to pymongo's create_index. Supported forms: - single field: ("email", 1) - compound: [("author_id", 1), ("created_at", -1)] - with options: {"keys": [("username", 1)], "options": {"unique": True}}

None
Example

ModelRegistry.register("users", UserDocument, indexes=[("email", 1)]) ModelRegistry.register( "posts", PostDocument, indexes=[[("author_id", 1), ("created_at", -1)]], )

set_cache_backend classmethod
set_cache_backend(backend: CacheBackend) -> None

Register a cache backend for all cache-enabled repositories.

Should be called after MongoDB connection is established, before cache-backed repositories are used.

Parameters:

Name Type Description Default
backend CacheBackend

A CacheBackend instance (InMemoryCacheBackend or RedisCacheBackend).

required
shutdown_cache async classmethod
shutdown_cache() -> None

Shutdown the registered cache backend gracefully.

Should be called during application shutdown to clean up background tasks (e.g., in-memory TTL cleanup).