{ "module": "mongo_ops.transactions", "content": { "path": "mongo_ops.transactions", "docstring": "# Summary\n\nTransaction management helpers for MongoDB.", "objects": { "MongoConnectionManager": { "name": "MongoConnectionManager", "kind": "class", "path": "mongo_ops.transactions.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.transactions.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.transactions.MongoConnectionManager.disconnect", "signature": "disconnect()", "docstring": "Close the active MongoDB connection and cleanup resources." }, "get_database": { "name": "get_database", "kind": "function", "path": "mongo_ops.transactions.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.transactions.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.transactions.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" } } }, "TransactionManager": { "name": "TransactionManager", "kind": "class", "path": "mongo_ops.transactions.TransactionManager", "signature": null, "docstring": "Simplified multi-document transaction handling.\n\nThis class provides helpers for executing operations within a MongoDB\ntransaction, ensuring ACID compliance for multi-document updates.", "members": { "start_session": { "name": "start_session", "kind": "function", "path": "mongo_ops.transactions.TransactionManager.start_session", "signature": "start_session(**kwargs: Any) -> AbstractAsyncContextManager[AsyncIOMotorClientSession]", "docstring": "Start a transaction session as an async context manager.\n\nNotes:\n Yields the active session with a started transaction; callers\n run their operations against the session inside the with-block.\n\nUsage:\n async with TransactionManager.start_session() as session:\n await collection.insert_one(doc, session=session)\n await other_collection.update_one(filter, update, session=session)" }, "execute_transaction": { "name": "execute_transaction", "kind": "function", "path": "mongo_ops.transactions.TransactionManager.execute_transaction", "signature": "execute_transaction(operations: list[Callable[[AsyncIOMotorClientSession], Awaitable[Any]]], **kwargs: Any) -> list[Any]", "docstring": "Execute multiple operations within a single transaction.\n\nArgs:\n operations (List[Callable[[AsyncIOMotorClientSession], Awaitable[Any]]]): A list of async\n callables that accept a session parameter and return a result.\n **kwargs: Transaction options.\n\nReturns:\n List[Any]: A list containing the results of each operation.\n\nExample:\n results = await TransactionManager.execute_transaction([\n lambda s: repo1.create(data1, session=s),\n lambda s: repo2.update(id, data2, session=s),\n ])" } } } } } }