{ "module": "mongo_ops.transactions", "content": { "path": "mongo_ops.transactions", "docstring": "Transaction management helpers for MongoDB.", "objects": { "Awaitable": { "name": "Awaitable", "kind": "alias", "path": "mongo_ops.transactions.Awaitable", "signature": "", "docstring": null }, "asynccontextmanager": { "name": "asynccontextmanager", "kind": "alias", "path": "mongo_ops.transactions.asynccontextmanager", "signature": "", "docstring": null }, "Any": { "name": "Any", "kind": "alias", "path": "mongo_ops.transactions.Any", "signature": "", "docstring": null }, "Callable": { "name": "Callable", "kind": "alias", "path": "mongo_ops.transactions.Callable", "signature": "", "docstring": null }, "List": { "name": "List", "kind": "alias", "path": "mongo_ops.transactions.List", "signature": "", "docstring": null }, "AsyncIOMotorClientSession": { "name": "AsyncIOMotorClientSession", "kind": "alias", "path": "mongo_ops.transactions.AsyncIOMotorClientSession", "signature": "", "docstring": null }, "MongoConnectionManager": { "name": "MongoConnectionManager", "kind": "class", "path": "mongo_ops.transactions.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.transactions.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.transactions.MongoConnectionManager.disconnect", "signature": "", "docstring": "Close the active MongoDB connection and cleanup resources." }, "get_database": { "name": "get_database", "kind": "function", "path": "mongo_ops.transactions.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.transactions.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.transactions.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" } } }, "TransactionManager": { "name": "TransactionManager", "kind": "class", "path": "mongo_ops.transactions.TransactionManager", "signature": "", "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": "", "docstring": "Start a transaction session as an async context manager.\n\nArgs:\n **kwargs: Transaction options (e.g., read_concern, write_concern).\n\nYields:\n AsyncIOMotorClientSession: The active session with a started transaction.\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": "", "docstring": "Execute multiple operations within a single transaction.\n\nArgs:\n operations: A list of async callables that accept a session \n parameter and return a result.\n **kwargs: Transaction options.\n\nReturns:\n List[Any]: A list containing the results of each operation.\n\nUsage:\n results = await TransactionManager.execute_transaction([\n lambda s: repo1.create(data1, session=s),\n lambda s: repo2.update(id, data2, session=s),\n ])" } } } } } }