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 the registered cache backend instance.
Returns:
| Type | Description |
|---|---|
CacheBackend | None
|
Optional[CacheBackend]: The cache backend, if registered. |
get_model
classmethod
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
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 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 all registered collection names.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of collection names. |
register
classmethod
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
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 the registered cache backend gracefully.
Should be called during application shutdown to clean up background tasks (e.g., in-memory TTL cleanup).