Spaces:
Sleeping
Sleeping
Download database.py from AndreaNotes1/ayn: direct link, hf CLI and curl.
- Browser
- Download file 827 Bytes
-
https://huggingface.co/spaces/AndreaNotes1/ayn/resolve/main/database.py
- Command line
-
hf download hf://spaces/AndreaNotes1/ayn/database.py
-
curl -L -o database.py https://huggingface.co/spaces/AndreaNotes1/ayn/resolve/main/database.py
827 Bytes
| """ | |
| MongoDB Atlas connection using Motor (async driver). | |
| """ | |
| from motor.motor_asyncio import AsyncIOMotorClient | |
| from config import settings | |
| client: AsyncIOMotorClient | None = None | |
| def get_client() -> AsyncIOMotorClient: | |
| global client | |
| if client is None: | |
| if not settings.MONGODB_URI: | |
| raise RuntimeError( | |
| "MONGODB_URI is not set. Add your MongoDB Atlas connection " | |
| "string as an environment variable / HF Space secret." | |
| ) | |
| client = AsyncIOMotorClient(settings.MONGODB_URI) | |
| return client | |
| def get_db(): | |
| return get_client()[settings.MONGODB_DB_NAME] | |
| def users_collection(): | |
| return get_db()["users"] | |
| def conversations_collection(): | |
| return get_db()["conversations"] | |
| def documents_collection(): | |
| return get_db()["documents"] | |