-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
56 lines (43 loc) · 1.65 KB
/
Copy pathdatabase.py
File metadata and controls
56 lines (43 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""MongoDB database connection management using Motor async driver.
This module handles async MongoDB connections for the application using Motor,
which provides async/await support for MongoDB operations in FastAPI.
"""
from motor.motor_asyncio import AsyncIOMotorClient
from motor.motor_asyncio import AsyncIOMotorDatabase
from config import settings
# Global database client and database instances
client: AsyncIOMotorClient = None
db: AsyncIOMotorDatabase = None
async def connect_to_mongo():
"""Establish connection to MongoDB database.
Initializes the global MongoDB client and database instance.
Tests the connection with a ping command to ensure it's working.
Raises:
Exception: If connection fails
"""
global client, db
try:
# Create async MongoDB client
client = AsyncIOMotorClient(settings.mongodb_url)
# Select database
db = client[settings.database_name]
# Test the connection by pinging the database
await client.admin.command('ping')
print(f"Connected to MongoDB: {settings.database_name}")
except Exception as e:
print(f"Failed to connect to MongoDB: {e}")
raise
async def close_mongo_connection():
"""Close MongoDB connection gracefully.
Called during application shutdown to clean up database connections.
"""
global client
if client:
client.close()
print("MongoDB connection closed")
def get_database() -> AsyncIOMotorDatabase:
"""Get the database instance for dependency injection.
Returns:
AsyncIOMotorDatabase: The active MongoDB database instance
"""
return db