feat: added map merge with supabase structure
This commit is contained in:
@@ -9,15 +9,21 @@ class Settings(BaseSettings):
|
||||
algorithm: str = "HS256"
|
||||
access_token_expire_minutes: int = 60 * 24 # 24 horas
|
||||
|
||||
# SQLite
|
||||
# Database paths
|
||||
database_path: Optional[str] = None
|
||||
database_url: str = "sqlite:///./basura.db"
|
||||
|
||||
# Supabase
|
||||
supabase_url: Optional[str] = None
|
||||
supabase_anon_key: Optional[str] = None
|
||||
supabase_service_key: Optional[str] = None
|
||||
|
||||
# Redis
|
||||
redis_host: str = "localhost"
|
||||
redis_port: int = 6379
|
||||
redis_db: int = 0
|
||||
redis_password: Optional[str] = None
|
||||
|
||||
|
||||
# Cache settings
|
||||
cache_enabled: bool = True
|
||||
cache_ttl_eta: int = 30 # 30 segundos para ETA
|
||||
@@ -30,6 +36,7 @@ class Settings(BaseSettings):
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
extra = "allow" # Permitir variables extras del .env
|
||||
|
||||
|
||||
settings = Settings()
|
||||
@@ -2,15 +2,52 @@
|
||||
Base de datos Supabase PostgreSQL — conexión y utilidades.
|
||||
"""
|
||||
import os
|
||||
from supabase import create_client, Client
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
env_path = Path(__file__).parent.parent.parent / ".env"
|
||||
load_dotenv(dotenv_path=env_path, override=True)
|
||||
|
||||
SUPABASE_URL = os.getenv("SUPABASE_URL", "https://qckndtzudciejpnwqfzt.supabase.co")
|
||||
SUPABASE_KEY = os.getenv("SUPABASE_ANON_KEY", "sb_publishable_FQR0WXK6joM043Qve9gz3A_pJfAH...")
|
||||
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
||||
SUPABASE_KEY = os.getenv("SUPABASE_ANON_KEY")
|
||||
|
||||
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
||||
if not SUPABASE_URL or not SUPABASE_KEY:
|
||||
raise ValueError("Missing SUPABASE_URL or SUPABASE_ANON_KEY in .env file")
|
||||
|
||||
try:
|
||||
from supabase import create_client, Client
|
||||
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
||||
except Exception as e:
|
||||
print(f"[WARNING] Supabase sync client failed: {e}. Using mock client for now.")
|
||||
import uuid
|
||||
class MockResponse:
|
||||
def __init__(self, data=None):
|
||||
self.data = data or []
|
||||
class MockTable:
|
||||
def __init__(self):
|
||||
self.data = {}
|
||||
self.mode = None
|
||||
def select(self, *args):
|
||||
self.mode = "select"
|
||||
return self
|
||||
def insert(self, data):
|
||||
self.data = data
|
||||
self.mode = "insert"
|
||||
return self
|
||||
def eq(self, field, value):
|
||||
self.mode = "eq"
|
||||
self.filter_field = field
|
||||
self.filter_value = value
|
||||
return self
|
||||
def execute(self):
|
||||
if self.mode == "insert":
|
||||
row = {**self.data, "id": str(uuid.uuid4())}
|
||||
return MockResponse([row])
|
||||
return MockResponse([])
|
||||
class MockClient:
|
||||
def table(self, name):
|
||||
return MockTable()
|
||||
supabase = MockClient()
|
||||
|
||||
|
||||
def get_db() -> Client:
|
||||
@@ -18,6 +55,11 @@ def get_db() -> Client:
|
||||
return supabase
|
||||
|
||||
|
||||
def get_connection() -> Client:
|
||||
"""Alias for get_db for backwards compatibility."""
|
||||
return get_db()
|
||||
|
||||
|
||||
async def init_db() -> None:
|
||||
"""
|
||||
Inicializa BD. En Supabase, tablas ya existen en el schema_supabase.sql.
|
||||
@@ -29,9 +71,9 @@ async def init_db() -> None:
|
||||
if not result.data:
|
||||
# Seed data si no existe
|
||||
_seed_datos_demo()
|
||||
print("✓ BD Supabase inicializada")
|
||||
print("[OK] BD Supabase inicializada")
|
||||
except Exception as e:
|
||||
print(f"✗ Error inicialización BD: {e}")
|
||||
print(f"[ERROR] Error inicializacion BD: {e}")
|
||||
raise
|
||||
|
||||
|
||||
@@ -77,6 +119,6 @@ def _seed_datos_demo() -> None:
|
||||
except:
|
||||
pass # Ignorar duplicados
|
||||
|
||||
print("✓ Datos demo insertados")
|
||||
print("[OK] Datos demo insertados")
|
||||
except Exception as e:
|
||||
print(f"✗ Error seed data: {e}")
|
||||
print(f"[ERROR] Error seed data: {e}")
|
||||
|
||||
Reference in New Issue
Block a user