{ "module": "mongo_ops.repository", "content": { "path": "mongo_ops.repository", "docstring": "Repository patterns and CRUD mixins for MongoDB.", "objects": { "datetime": { "name": "datetime", "kind": "alias", "path": "mongo_ops.repository.datetime", "signature": "", "docstring": null }, "Any": { "name": "Any", "kind": "alias", "path": "mongo_ops.repository.Any", "signature": "", "docstring": null }, "Dict": { "name": "Dict", "kind": "alias", "path": "mongo_ops.repository.Dict", "signature": "", "docstring": null }, "Generic": { "name": "Generic", "kind": "alias", "path": "mongo_ops.repository.Generic", "signature": "", "docstring": null }, "List": { "name": "List", "kind": "alias", "path": "mongo_ops.repository.List", "signature": "", "docstring": null }, "Optional": { "name": "Optional", "kind": "alias", "path": "mongo_ops.repository.Optional", "signature": "", "docstring": null }, "TypeVar": { "name": "TypeVar", "kind": "alias", "path": "mongo_ops.repository.TypeVar", "signature": "", "docstring": null }, "Union": { "name": "Union", "kind": "alias", "path": "mongo_ops.repository.Union", "signature": "", "docstring": null }, "ObjectId": { "name": "ObjectId", "kind": "alias", "path": "mongo_ops.repository.ObjectId", "signature": "", "docstring": null }, "AsyncIOMotorCollection": { "name": "AsyncIOMotorCollection", "kind": "alias", "path": "mongo_ops.repository.AsyncIOMotorCollection", "signature": "", "docstring": null }, "MongoConnectionManager": { "name": "MongoConnectionManager", "kind": "class", "path": "mongo_ops.repository.MongoConnectionManager", "signature": "", "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.repository.MongoConnectionManager.connect", "signature": "", "docstring": "Connect to MongoDB and initialize the shared client.\n\nArgs:\n uri: MongoDB connection URI (e.g., \"mongodb://localhost:27017\").\n db_name: 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.repository.MongoConnectionManager.disconnect", "signature": "", "docstring": "Close the active MongoDB connection and cleanup resources." }, "get_database": { "name": "get_database", "kind": "function", "path": "mongo_ops.repository.MongoConnectionManager.get_database", "signature": "", "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.repository.MongoConnectionManager.get_client", "signature": "", "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.repository.MongoConnectionManager.lifespan", "signature": "", "docstring": "Async context manager for managing connection lifecycle.\n\nDesigned for use with FastAPI or other frameworks supporting \nlifespan management.\n\nArgs:\n uri: MongoDB connection URI.\n db_name: Name of the database.\n **kwargs: Additional Motor client options.\n\nYields:\n AsyncIOMotorDatabase: The active database instance.\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.repository.BaseDocument", "signature": "", "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.repository.BaseDocument.id", "signature": "", "docstring": null }, "created_at": { "name": "created_at", "kind": "attribute", "path": "mongo_ops.repository.BaseDocument.created_at", "signature": "", "docstring": null }, "updated_at": { "name": "updated_at", "kind": "attribute", "path": "mongo_ops.repository.BaseDocument.updated_at", "signature": "", "docstring": null }, "Config": { "name": "Config", "kind": "class", "path": "mongo_ops.repository.BaseDocument.Config", "signature": "", "docstring": null, "members": { "populate_by_name": { "name": "populate_by_name", "kind": "attribute", "path": "mongo_ops.repository.BaseDocument.Config.populate_by_name", "signature": "", "docstring": null }, "arbitrary_types_allowed": { "name": "arbitrary_types_allowed", "kind": "attribute", "path": "mongo_ops.repository.BaseDocument.Config.arbitrary_types_allowed", "signature": "", "docstring": null }, "json_encoders": { "name": "json_encoders", "kind": "attribute", "path": "mongo_ops.repository.BaseDocument.Config.json_encoders", "signature": "", "docstring": null }, "json_schema_extra": { "name": "json_schema_extra", "kind": "attribute", "path": "mongo_ops.repository.BaseDocument.Config.json_schema_extra", "signature": "", "docstring": null } } } } }, "PopulationEngine": { "name": "PopulationEngine", "kind": "class", "path": "mongo_ops.repository.PopulationEngine", "signature": "", "docstring": null, "members": { "register_repo": { "name": "register_repo", "kind": "function", "path": "mongo_ops.repository.PopulationEngine.register_repo", "signature": "", "docstring": null }, "populate": { "name": "populate", "kind": "function", "path": "mongo_ops.repository.PopulationEngine.populate", "signature": "", "docstring": null }, "depopulate": { "name": "depopulate", "kind": "function", "path": "mongo_ops.repository.PopulationEngine.depopulate", "signature": "", "docstring": null } } }, "PopulateRule": { "name": "PopulateRule", "kind": "class", "path": "mongo_ops.repository.PopulateRule", "signature": "", "docstring": null, "members": { "field_name": { "name": "field_name", "kind": "attribute", "path": "mongo_ops.repository.PopulateRule.field_name", "signature": "", "docstring": null }, "collection_name": { "name": "collection_name", "kind": "attribute", "path": "mongo_ops.repository.PopulateRule.collection_name", "signature": "", "docstring": null }, "nested_rules": { "name": "nested_rules", "kind": "attribute", "path": "mongo_ops.repository.PopulateRule.nested_rules", "signature": "", "docstring": null }, "max_depth": { "name": "max_depth", "kind": "attribute", "path": "mongo_ops.repository.PopulateRule.max_depth", "signature": "", "docstring": null }, "filter": { "name": "filter", "kind": "attribute", "path": "mongo_ops.repository.PopulateRule.filter", "signature": "", "docstring": null }, "projection": { "name": "projection", "kind": "attribute", "path": "mongo_ops.repository.PopulateRule.projection", "signature": "", "docstring": null } } }, "T": { "name": "T", "kind": "attribute", "path": "mongo_ops.repository.T", "signature": null, "docstring": null }, "CRUDMixin": { "name": "CRUDMixin", "kind": "class", "path": "mongo_ops.repository.CRUDMixin", "signature": "", "docstring": "Generic CRUD operations mixin for MongoDB collections.\n\nThis mixin provides standard Create, Read, Update, and Delete operations \nthat work with Pydantic models.\n\nAttributes:\n collection: The Motor collection instance.\n model: The Pydantic model class representing the document.", "members": { "collection": { "name": "collection", "kind": "attribute", "path": "mongo_ops.repository.CRUDMixin.collection", "signature": null, "docstring": null }, "model": { "name": "model", "kind": "attribute", "path": "mongo_ops.repository.CRUDMixin.model", "signature": null, "docstring": null }, "data_to_model": { "name": "data_to_model", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.data_to_model", "signature": "", "docstring": null }, "create": { "name": "create", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.create", "signature": "", "docstring": "Create a new document in the collection.\n\nArgs:\n data: The Pydantic model instance to insert.\n\nReturns:\n T: The created Pydantic model instance, including the assigned ID." }, "get_by_id": { "name": "get_by_id", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.get_by_id", "signature": "", "docstring": "Retrieve a document by its ID.\n\nArgs:\n id: The document ID (string or ObjectId).\n\nReturns:\n Optional[T]: The Pydantic model instance if found, else None." }, "get_many": { "name": "get_many", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.get_many", "signature": "", "docstring": "Retrieve multiple documents with filtering, pagination, and sorting.\n\nArgs:\n filter: MongoDB filter dictionary (e.g., {\"is_active\": True}).\n skip: Number of documents to skip for pagination.\n limit: Maximum number of documents to return (default 100).\n sort: List of sort specifications [(field, direction), ...].\n E.g., [(\"created_at\", -1)] for descending.\n\nReturns:\n List[T]: A list of Pydantic model instances.\n\nUsage:\n ```python\n users = await repo.get_many(\n filter={\"role\": \"admin\"},\n limit=10,\n sort=[(\"username\", 1)]\n )\n ```" }, "update": { "name": "update", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.update", "signature": "", "docstring": "Update a document by its ID using the $set operator.\n\nArgs:\n id: The document ID (string or ObjectId).\n data: A dictionary of fields and values to update.\n\nReturns:\n Optional[T]: The updated Pydantic model instance if found, else None.\n\nUsage:\n ```python\n updated_user = await repo.update(user_id, {\"email\": \"new@example.com\"})\n ```" }, "patch": { "name": "patch", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.patch", "signature": "", "docstring": "Partially update a document using $set (REST PATCH semantics).\n\nUnlike update(), patch() takes a partial dict and applies only those\nfields. PopulatingRepository overrides this to prevent patching FK fields.\n\nArgs:\n id: The document ID (string or ObjectId).\n data: A partial dictionary of fields and values to update.\n\nReturns:\n Optional[T]: The updated Pydantic model instance if found, else None." }, "delete": { "name": "delete", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.delete", "signature": "", "docstring": "Delete a document by its ID.\n\nArgs:\n id: The document ID (string or ObjectId).\n\nReturns:\n bool: True if a document was deleted, False otherwise." }, "count": { "name": "count", "kind": "function", "path": "mongo_ops.repository.CRUDMixin.count", "signature": "", "docstring": "Count documents matching a filter.\n\nArgs:\n filter: MongoDB filter dictionary.\n\nReturns:\n int: The number of matching documents." } } }, "BaseRepository": { "name": "BaseRepository", "kind": "class", "path": "mongo_ops.repository.BaseRepository", "signature": "", "docstring": "Base repository class combining CRUD operations and collection management.\n\nThis class simplifies repository creation by automatically obtaining the \ndatabase connection and collection instance.\n\nAttributes:\n collection_name: The name of the collection managed by this repository.", "members": { "collection_name": { "name": "collection_name", "kind": "attribute", "path": "mongo_ops.repository.BaseRepository.collection_name", "signature": null, "docstring": null } } }, "PopulatingRepository": { "name": "PopulatingRepository", "kind": "class", "path": "mongo_ops.repository.PopulatingRepository", "signature": "", "docstring": null, "members": { "population_engine": { "name": "population_engine", "kind": "attribute", "path": "mongo_ops.repository.PopulatingRepository.population_engine", "signature": null, "docstring": null }, "set_population_engine": { "name": "set_population_engine", "kind": "function", "path": "mongo_ops.repository.PopulatingRepository.set_population_engine", "signature": "", "docstring": null }, "set_populate_rules": { "name": "set_populate_rules", "kind": "function", "path": "mongo_ops.repository.PopulatingRepository.set_populate_rules", "signature": "", "docstring": null }, "data_to_model": { "name": "data_to_model", "kind": "function", "path": "mongo_ops.repository.PopulatingRepository.data_to_model", "signature": "", "docstring": null }, "create": { "name": "create", "kind": "function", "path": "mongo_ops.repository.PopulatingRepository.create", "signature": "", "docstring": null }, "update": { "name": "update", "kind": "function", "path": "mongo_ops.repository.PopulatingRepository.update", "signature": "", "docstring": null }, "patch": { "name": "patch", "kind": "function", "path": "mongo_ops.repository.PopulatingRepository.patch", "signature": "", "docstring": null } } } } } }