Skip to content

Core Components

Core Components

1. MongoConnectionManager

Manages MongoDB connections with async lifecycle. Methods:

  • connect(uri, db_name, **kwargs) - Connect to MongoDB
  • disconnect() - Close connection
  • get_database() - Get current database instance
  • get_client() - Get current client instance
  • lifespan(uri, db_name, **kwargs) - Context manager for FastAPI lifespan

2. BaseDocument

Base model for all MongoDB documents. Provides:

  • id (aliased to _id) - ObjectId
  • created_at - Auto-generated timestamp
  • updated_at - Auto-updated timestamp

3. BaseRepository[T]

Generic repository with CRUD operations:

  • create(data: T) -> T
  • get_by_id(id: str | ObjectId) -> Optional[T]
  • get_many(filter, skip, limit, sort) -> List[T]
  • update(id, data: Dict) -> Optional[T]
  • delete(id) -> bool
  • count(filter) -> int

4. TransactionManager

Handles multi-document transactions:

  • start_session() - Context manager for transactions
  • execute_transaction(operations) - Execute multiple operations atomically

5. ModelRegistry

Register and initialize models:

  • register(collection_name, model, indexes) - Register a model
  • initialize_all() - Create all indexes
  • get_model(collection_name) - Get registered model
  • list_collections() - List all registered collections