#modules/database/database_init.py | |
import os | |
from azure.cosmos import CosmosClient | |
import logging | |
logger = logging.getLogger(__name__) | |
# Conexi贸n global | |
db_client = None | |
def initialize_database_connections(): | |
"""Inicializa la conexi贸n a Cosmos DB.""" | |
global db_client | |
try: | |
endpoint = os.getenv("COSMOS_ENDPOINT") | |
key = os.getenv("COSMOS_KEY") | |
if not endpoint or not key: | |
raise ValueError("Las variables de entorno COSMOS_ENDPOINT y COSMOS_KEY no est谩n configuradas.") | |
db_client = CosmosClient(endpoint, key) | |
logger.info("Conexi贸n a Cosmos DB inicializada correctamente.") | |
return True | |
except Exception as e: | |
logger.error(f"Error inicializando conexi贸n a Cosmos DB: {e}") | |
return False | |
def get_database_client(database_name): | |
"""Devuelve el cliente de la base de datos especificada.""" | |
if not db_client: | |
raise ValueError("La conexi贸n a la base de datos no est谩 inicializada.") | |
return db_client.get_database_client(database_name) | |