Create modules/database/database_init.py
Browse files
modules/database/database_init.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#modules/database/database_init.py
|
2 |
+
import os
|
3 |
+
from azure.cosmos import CosmosClient
|
4 |
+
import logging
|
5 |
+
|
6 |
+
logger = logging.getLogger(__name__)
|
7 |
+
|
8 |
+
# Conexi贸n global
|
9 |
+
db_client = None
|
10 |
+
|
11 |
+
def initialize_database_connections():
|
12 |
+
"""Inicializa la conexi贸n a Cosmos DB."""
|
13 |
+
global db_client
|
14 |
+
try:
|
15 |
+
endpoint = os.getenv("COSMOS_ENDPOINT")
|
16 |
+
key = os.getenv("COSMOS_KEY")
|
17 |
+
if not endpoint or not key:
|
18 |
+
raise ValueError("Las variables de entorno COSMOS_ENDPOINT y COSMOS_KEY no est谩n configuradas.")
|
19 |
+
|
20 |
+
db_client = CosmosClient(endpoint, key)
|
21 |
+
logger.info("Conexi贸n a Cosmos DB inicializada correctamente.")
|
22 |
+
return True
|
23 |
+
except Exception as e:
|
24 |
+
logger.error(f"Error inicializando conexi贸n a Cosmos DB: {e}")
|
25 |
+
return False
|
26 |
+
|
27 |
+
def get_database_client(database_name):
|
28 |
+
"""Devuelve el cliente de la base de datos especificada."""
|
29 |
+
if not db_client:
|
30 |
+
raise ValueError("La conexi贸n a la base de datos no est谩 inicializada.")
|
31 |
+
return db_client.get_database_client(database_name)
|