Spaces:
Sleeping
Sleeping
Download src/db.py from Arclen/ucprojects-backend: direct link, hf CLI and curl.
- Browser
- Download file 752 Bytes
-
https://huggingface.co/spaces/Arclen/ucprojects-backend/resolve/main/src/db.py
- Command line
-
hf download hf://spaces/Arclen/ucprojects-backend/src/db.py
-
curl -L -o db.py https://huggingface.co/spaces/Arclen/ucprojects-backend/resolve/main/src/db.py
752 Bytes
| from motor.motor_asyncio import AsyncIOMotorClient | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment from .env if present | |
| load_dotenv() | |
| MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017/") | |
| # Optional database name (useful for MongoDB SRV URIs that don't include a DB) | |
| MONGODB_DB = os.getenv("MONGODB_DB", "ucprojects") | |
| client: AsyncIOMotorClient | None = None | |
| def get_client() -> AsyncIOMotorClient: | |
| global client | |
| if client is None: | |
| client = AsyncIOMotorClient(MONGODB_URI) | |
| return client | |
| def get_db(): | |
| # Return a named database. If the URI already contains a default DB, this | |
| # will still work because get_database accepts the explicit name. | |
| return get_client().get_database(MONGODB_DB) | |