{ "module": "mongo_ops.registry", "content": { "path": "mongo_ops.registry", "docstring": "# Summary\n\nModel registration for multi-service initialization.", "objects": { "CacheBackend": { "name": "CacheBackend", "kind": "class", "path": "mongo_ops.registry.CacheBackend", "signature": null, "docstring": "Abstract interface for cache backends.\n\nImplementations store byte-encoded values keyed by string, track usage\nstatistics, and manage their own lifecycle. The in-memory and Redis\nbackends both implement this contract.", "members": { "get": { "name": "get", "kind": "function", "path": "mongo_ops.registry.CacheBackend.get", "signature": "get(key: str)", "docstring": "Fetch a value from the cache.\n\nArgs:\n key (str):\n The cache key.\n\nReturns:\n Optional[bytes]: The cached bytes, or None on a miss." }, "set": { "name": "set", "kind": "function", "path": "mongo_ops.registry.CacheBackend.set", "signature": "set(key: str, value: bytes, ttl: int | None = None)", "docstring": "Store a value in the cache.\n\nArgs:\n key (str):\n The cache key.\n value (bytes):\n The byte-encoded value to store.\n ttl (Optional[int]):\n Time-to-live in seconds. When None, the backend default\n applies." }, "delete": { "name": "delete", "kind": "function", "path": "mongo_ops.registry.CacheBackend.delete", "signature": "delete(key: str)", "docstring": "Remove a key from the cache.\n\nArgs:\n key (str):\n The cache key." }, "exists": { "name": "exists", "kind": "function", "path": "mongo_ops.registry.CacheBackend.exists", "signature": "exists(key: str)", "docstring": "Check whether a key is present.\n\nArgs:\n key (str):\n The cache key.\n\nReturns:\n bool: True if the key exists, False otherwise." }, "clear_pattern": { "name": "clear_pattern", "kind": "function", "path": "mongo_ops.registry.CacheBackend.clear_pattern", "signature": "clear_pattern(pattern: str)", "docstring": "Remove all keys matching a glob pattern.\n\nArgs:\n pattern (str):\n Glob-style pattern; a trailing `*` matches prefixes." }, "get_stats": { "name": "get_stats", "kind": "function", "path": "mongo_ops.registry.CacheBackend.get_stats", "signature": "get_stats()", "docstring": "Return a snapshot of cache statistics.\n\nReturns:\n CacheStats: A copy of the current stats counters." }, "initialize": { "name": "initialize", "kind": "function", "path": "mongo_ops.registry.CacheBackend.initialize", "signature": "initialize()", "docstring": "Start background resources owned by the backend.\n\nShould be called once during application startup, after the\nrepositories are connected." }, "shutdown": { "name": "shutdown", "kind": "function", "path": "mongo_ops.registry.CacheBackend.shutdown", "signature": "shutdown()", "docstring": "Stop and release background resources.\n\nShould be called once during application shutdown." } } }, "MongoConnectionManager": { "name": "MongoConnectionManager", "kind": "class", "path": "mongo_ops.registry.MongoConnectionManager", "signature": null, "docstring": "Manages MongoDB connections with async lifecycle.\n\nThis class provides a singleton-like manager for the MongoDB client and\ndatabase instances, ensuring they are properly initialized and closed\nacross the application lifecycle.", "members": { "connect": { "name": "connect", "kind": "function", "path": "mongo_ops.registry.MongoConnectionManager.connect", "signature": "connect(uri: str, db_name: str, **kwargs: Any)", "docstring": "Connect to MongoDB and initialize the shared client.\n\nArgs:\n uri (str): MongoDB connection URI (e.g., \"mongodb://localhost:27017\").\n db_name (str): Name of the database to use.\n **kwargs: Additional Motor client options (e.g., maxPoolSize).\n\nReturns:\n AsyncIOMotorDatabase: The initialized database instance." }, "disconnect": { "name": "disconnect", "kind": "function", "path": "mongo_ops.registry.MongoConnectionManager.disconnect", "signature": "disconnect()", "docstring": "Close the active MongoDB connection and cleanup resources." }, "get_database": { "name": "get_database", "kind": "function", "path": "mongo_ops.registry.MongoConnectionManager.get_database", "signature": "get_database()", "docstring": "Retrieve the current database instance.\n\nReturns:\n AsyncIOMotorDatabase: The active database instance.\n\nRaises:\n RuntimeError: If connect() has not been called yet." }, "get_client": { "name": "get_client", "kind": "function", "path": "mongo_ops.registry.MongoConnectionManager.get_client", "signature": "get_client()", "docstring": "Retrieve the current client instance.\n\nReturns:\n AsyncIOMotorClient: The active Motor client instance.\n\nRaises:\n RuntimeError: If connect() has not been called yet." }, "lifespan": { "name": "lifespan", "kind": "function", "path": "mongo_ops.registry.MongoConnectionManager.lifespan", "signature": "lifespan(uri: str, db_name: str, **kwargs: Any)", "docstring": "Async context manager for managing connection lifecycle.\n\nDesigned for use with FastAPI or other frameworks supporting\nlifespan management.\n\nNotes:\n Connects to MongoDB on entry, yields the active database instance\n to the context body, and disconnects on exit.\n\nUsage:\n @asynccontextmanager\n async def lifespan(app: FastAPI):\n async with MongoConnectionManager.lifespan(uri, db_name):\n yield" } } }, "BaseDocument": { "name": "BaseDocument", "kind": "class", "path": "mongo_ops.registry.BaseDocument", "signature": null, "docstring": "Base document class with common MongoDB fields.\n\nInherit from this class to create Pydantic models that represent\nMongoDB documents. It includes automatic handling of the `_id` field\nand timestamps.\n\nAttributes:\n id: The MongoDB document ID (aliased to `_id`).\n created_at: Timestamp when the document was created.\n updated_at: Timestamp when the document was last updated.", "members": { "id": { "name": "id", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.id", "signature": null, "docstring": null }, "created_at": { "name": "created_at", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.created_at", "signature": null, "docstring": null }, "updated_at": { "name": "updated_at", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.updated_at", "signature": null, "docstring": null }, "Config": { "name": "Config", "kind": "class", "path": "mongo_ops.registry.BaseDocument.Config", "signature": null, "docstring": null, "members": { "populate_by_name": { "name": "populate_by_name", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.Config.populate_by_name", "signature": null, "docstring": null }, "arbitrary_types_allowed": { "name": "arbitrary_types_allowed", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.Config.arbitrary_types_allowed", "signature": null, "docstring": null }, "json_encoders": { "name": "json_encoders", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.Config.json_encoders", "signature": null, "docstring": null }, "json_schema_extra": { "name": "json_schema_extra", "kind": "attribute", "path": "mongo_ops.registry.BaseDocument.Config.json_schema_extra", "signature": null, "docstring": null } } } } }, "ModelRegistry": { "name": "ModelRegistry", "kind": "class", "path": "mongo_ops.registry.ModelRegistry", "signature": null, "docstring": "Registry for managing multiple models and their collections.\n\nThis registry allows central management of collections and their\nassociated indexes, making it easier to perform mass initialization\nat application startup.", "members": { "register": { "name": "register", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.register", "signature": "register(collection_name: str, model: type[BaseDocument], indexes: list[tuple] | None = None) -> None", "docstring": "Register a model with its collection and indexes.\n\nArgs:\n collection_name (str): Name of the MongoDB collection.\n model (type[BaseDocument]): Document model class (subclass of BaseDocument).\n indexes (list[Any]): List of index specifications. Each spec is passed\n directly to pymongo's create_index. Supported forms:\n - single field: (\"email\", 1)\n - compound: [(\"author_id\", 1), (\"created_at\", -1)]\n - with options: {\"keys\": [(\"username\", 1)], \"options\": {\"unique\": True}}\n\nExample:\n ModelRegistry.register(\"users\", UserDocument, indexes=[(\"email\", 1)])\n ModelRegistry.register(\n \"posts\",\n PostDocument,\n indexes=[[(\"author_id\", 1), (\"created_at\", -1)]],\n )" }, "initialize_all": { "name": "initialize_all", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.initialize_all", "signature": "initialize_all(db: AsyncIOMotorDatabase | None = None) -> None", "docstring": "Initialize all registered collections and create indexes.\n\nThis method should be called during application startup to ensure\nall necessary indexes exist in the database.\n\nArgs:\n db (Optional[AsyncIOMotorDatabase]): Database instance. If not provided,\n uses the global database from MongoConnectionManager." }, "get_model": { "name": "get_model", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.get_model", "signature": "get_model(collection_name: str) -> type[BaseDocument]", "docstring": "Retrieve a registered model by its collection name.\n\nArgs:\n collection_name (str): The name of the collection.\n\nReturns:\n type[BaseDocument]: The registered model class.\n\nRaises:\n KeyError: If the model for the given collection is not registered." }, "list_collections": { "name": "list_collections", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.list_collections", "signature": "list_collections() -> list[str]", "docstring": "List all registered collection names.\n\nReturns:\n list[str]: A list of collection names." }, "get_cache_backend": { "name": "get_cache_backend", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.get_cache_backend", "signature": "get_cache_backend() -> CacheBackend | None", "docstring": "Get the registered cache backend instance.\n\nReturns:\n Optional[CacheBackend]: The cache backend, if registered." }, "set_cache_backend": { "name": "set_cache_backend", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.set_cache_backend", "signature": "set_cache_backend(backend: CacheBackend) -> None", "docstring": "Register a cache backend for all cache-enabled repositories.\n\nShould be called after MongoDB connection is established,\nbefore cache-backed repositories are used.\n\nArgs:\n backend (CacheBackend): A CacheBackend instance (InMemoryCacheBackend\n or RedisCacheBackend)." }, "initialize_cache": { "name": "initialize_cache", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.initialize_cache", "signature": "initialize_cache() -> None", "docstring": "Initialize the registered cache backend.\n\nMust be called after set_cache_backend() and before any\ncache-backed repository operations. Typically called right\nafter MongoDB connection is established.\n\nRaises:\n RuntimeError: If no cache backend has been registered." }, "shutdown_cache": { "name": "shutdown_cache", "kind": "function", "path": "mongo_ops.registry.ModelRegistry.shutdown_cache", "signature": "shutdown_cache() -> None", "docstring": "Shutdown the registered cache backend gracefully.\n\nShould be called during application shutdown to clean up\nbackground tasks (e.g., in-memory TTL cleanup)." } } } } } }