44from dataclasses import dataclass
55from typing import Any , AsyncContextManager , Protocol , TypeVar , runtime_checkable
66
7- from motor .motor_asyncio import (
8- AsyncIOMotorClient ,
9- AsyncIOMotorClientSession ,
10- AsyncIOMotorCollection ,
11- AsyncIOMotorCursor ,
12- AsyncIOMotorDatabase ,
13- )
7+ from pymongo .asynchronous .client_session import AsyncClientSession
8+ from pymongo .asynchronous .collection import AsyncCollection
9+ from pymongo .asynchronous .cursor import AsyncCursor
10+ from pymongo .asynchronous .database import AsyncDatabase
11+ from pymongo .asynchronous .mongo_client import AsyncMongoClient
1412from pymongo .errors import ServerSelectionTimeoutError
1513
1614from app .core .logging import logger
1715
1816# Python 3.12 type aliases using the new 'type' statement
19- # MongoDocument represents the raw document type returned by Motor operations
17+ # MongoDocument represents the raw document type returned by PyMongo operations
2018type MongoDocument = dict [str , Any ]
21- type DBClient = AsyncIOMotorClient [MongoDocument ]
22- type Database = AsyncIOMotorDatabase [MongoDocument ]
23- type Collection = AsyncIOMotorCollection [MongoDocument ]
24- type Cursor = AsyncIOMotorCursor [MongoDocument ]
25- type DBSession = AsyncIOMotorClientSession
19+ type DBClient = AsyncMongoClient [MongoDocument ]
20+ type Database = AsyncDatabase [MongoDocument ]
21+ type Collection = AsyncCollection [MongoDocument ]
22+ type Cursor = AsyncCursor [MongoDocument ]
23+ type DBSession = AsyncClientSession
2624
2725# Type variable for generic database provider
2826T = TypeVar ("T" )
@@ -106,10 +104,8 @@ async def connect(self) -> None:
106104
107105 logger .info (f"Connecting to MongoDB database: { self ._db_name } " )
108106
109- # Always explicitly bind to current event loop for consistency
110- import asyncio
111-
112- client : DBClient = AsyncIOMotorClient (
107+ # PyMongo Async automatically uses the current event loop
108+ client : DBClient = AsyncMongoClient (
113109 self ._config .mongodb_url ,
114110 serverSelectionTimeoutMS = self ._config .server_selection_timeout_ms ,
115111 connectTimeoutMS = self ._config .connect_timeout_ms ,
@@ -119,7 +115,6 @@ async def connect(self) -> None:
119115 retryReads = self ._config .retry_reads ,
120116 w = self ._config .write_concern ,
121117 journal = self ._config .journal ,
122- io_loop = asyncio .get_running_loop (), # Always bind to current loop
123118 )
124119
125120 # Verify connection
@@ -128,7 +123,7 @@ async def connect(self) -> None:
128123 logger .info ("Successfully connected to MongoDB" )
129124 except ServerSelectionTimeoutError as e :
130125 logger .error (f"Failed to connect to MongoDB: { e } " )
131- client .close ()
126+ await client .close ()
132127 raise
133128
134129 self ._client = client
@@ -137,7 +132,7 @@ async def connect(self) -> None:
137132 async def disconnect (self ) -> None :
138133 if self ._client is not None :
139134 logger .info ("Closing MongoDB connection" )
140- self ._client .close ()
135+ await self ._client .close ()
141136 self ._client = None
142137 self ._database = None
143138
0 commit comments