File size: 1,058 Bytes
a13119d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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)