File size: 1,628 Bytes
9513328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Supabase client singleton — single source of truth.
Replaces scattered supabase client creation across routers.

Usage:
    from app.core.db import get_supabase
    client = await get_supabase()
    result = await client.table("alerts").select("*").execute()
"""

from __future__ import annotations

import logging
import os
from typing import Any

logger = logging.getLogger(__name__)

_client: Any = None


async def get_supabase() -> Any:
    """Get or create the async Supabase client."""
    global _client
    if _client is not None:
        return _client

    url = os.getenv("SUPABASE_URL", "")
    key = os.getenv("SUPABASE_SERVICE_KEY", "") or os.getenv("SUPABASE_KEY", "")

    if not url or not key:
        logger.warning("supabase_not_configured")
        return None

    try:
        from supabase import create_client as _create_sync
        _client = _create_sync(url, key)
        logger.info("supabase_connected", url=url[:30])
        return _client
    except ImportError:
        logger.error("supabase_package_missing — pip install supabase")
        return None
    except Exception as e:
        logger.error("supabase_init_failed", error=str(e))
        return None


def get_supabase_sync() -> Any:
    """Synchronous Supabase client for scripts and non-async contexts."""
    url = os.getenv("SUPABASE_URL", "")
    key = os.getenv("SUPABASE_SERVICE_KEY", "") or os.getenv("SUPABASE_KEY", "")

    if not url or not key:
        return None

    try:
        from supabase import create_client
        return create_client(url, key)
    except (ImportError, Exception):
        return None