Spaces:
Sleeping
Sleeping
Download database/mongodb.py from benedictpepper/fitmate-api: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/benedictpepper/fitmate-api/resolve/main/database/mongodb.py
- Command line
-
hf download hf://spaces/benedictpepper/fitmate-api/database/mongodb.py
-
curl -L -o mongodb.py https://huggingface.co/spaces/benedictpepper/fitmate-api/resolve/main/database/mongodb.py
1.49 kB
| """ | |
| mongodb.py β MongoDB connection and collection access for FitMate | |
| Uses pymongo (sync) for seed scripts. Motor (async) will wrap this for FastAPI. | |
| """ | |
| import os | |
| from pymongo import MongoClient | |
| from pymongo.collection import Collection | |
| from pymongo.database import Database | |
| try: | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| except ImportError: | |
| pass | |
| MONGODB_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017") | |
| MONGODB_DB_NAME = os.getenv("MONGODB_DB_NAME", "fitmate_db") | |
| def get_db() -> Database: | |
| """Get synchronous MongoDB database instance.""" | |
| client = MongoClient(MONGODB_URL) | |
| return client[MONGODB_DB_NAME] | |
| def get_collection(collection_name: str) -> Collection: | |
| """Get a specific collection from the database.""" | |
| db = get_db() | |
| return db[collection_name] | |
| def create_indexes(): | |
| """Create MongoDB indexes for performance. Run once during setup.""" | |
| db = get_db() | |
| db["tcm_ingredients"].create_index("mandarin_name") | |
| db["tcm_ingredients"].create_index("indonesian_name") | |
| db["tcm_ingredients"].create_index("latin_name") | |
| db["tcm_ingredients"].create_index([("mandarin_name", "text"), ("indonesian_name", "text")]) | |
| db["safety_rules"].create_index("ingredient_id") | |
| db["safety_rules"].create_index("condition_logic") | |
| db["scan_logs"].create_index("scanned_at") | |
| db["scan_logs"].create_index("warning_triggered") | |
| print("[ok] MongoDB indexes created") | |