39 lines
892 B
Python
39 lines
892 B
Python
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
|
from pymongo.server_api import ServerApi
|
|
from app.config import get_settings
|
|
|
|
settings = get_settings()
|
|
|
|
client: AsyncIOMotorClient = None
|
|
db = None
|
|
gridfs_bucket: AsyncIOMotorGridFSBucket = None
|
|
|
|
|
|
async def connect_db():
|
|
global client, db, gridfs_bucket
|
|
client = AsyncIOMotorClient(
|
|
settings.mongodb_uri,
|
|
server_api=ServerApi("1"),
|
|
)
|
|
db = client[settings.mongodb_db]
|
|
gridfs_bucket = AsyncIOMotorGridFSBucket(db, bucket_name="resumes")
|
|
# Ping to verify connection
|
|
await client.admin.command("ping")
|
|
print(f"[OK] Connected to MongoDB: {settings.mongodb_db}")
|
|
|
|
|
|
|
|
async def close_db():
|
|
global client
|
|
if client:
|
|
client.close()
|
|
print("[CLOSED] MongoDB connection closed")
|
|
|
|
|
|
|
|
def get_db():
|
|
return db
|
|
|
|
|
|
def get_gridfs():
|
|
return gridfs_bucket
|