File size: 2,682 Bytes
a37d0db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""

Supabase Database Connection Module



Handles connection to Supabase PostgreSQL database using the Supabase REST API.

Credentials are read from config.py environment variables.

"""

import requests
from ..config import SUPABASE_URL, SUPABASE_SERVICE_KEY


class Database:
    """

    Supabase database connection manager.

    

    Uses the Supabase REST API directly via requests.

    Service key provides admin-level access from backend.

    """
    
    def __init__(self, url: str = SUPABASE_URL, key: str = SUPABASE_SERVICE_KEY):
        """

        Initialize Supabase connection.

        

        Args:

            url: Supabase project URL.

            key: Supabase service key (for server-side admin access).

        

        Raises:

            ValueError: If credentials are missing.

        """
        if not url or not key:
            raise ValueError("SUPABASE_URL and SUPABASE_SERVICE_KEY are required")

        self.base_url = url.rstrip("/")
        self.headers = {
            "apikey": key,
            "Authorization": f"Bearer {key}",
            "Content-Type": "application/json"
        }
        self.session = requests.Session()

        print(f"Supabase connection configured: {self.base_url}")

    def close(self):
        """

        Close the underlying HTTP session.

        """
        self.session.close()

    def close_all(self):
        """

        Alias for backward compatibility with shutdown cleanup.

        """
        self.close()

    def insert(self, table: str, data: dict) -> list:
        """

        Insert a record into a Supabase table and return the inserted row.

        """
        url = f"{self.base_url}/rest/v1/{table}"
        headers = self.headers.copy()
        headers["Prefer"] = "return=representation"

        response = self.session.post(url, json=data, headers=headers)
        if response.status_code not in (200, 201):
            raise RuntimeError(f"Supabase insert failed ({response.status_code}): {response.text}")

        return response.json()

    def query(self, table: str, select: str = "*", params: dict = None) -> list:
        """

        Run a simple select query against a Supabase table.

        """
        url = f"{self.base_url}/rest/v1/{table}"
        query_params = {"select": select}
        if params:
            query_params.update(params)

        response = self.session.get(url, headers=self.headers, params=query_params)
        if response.status_code != 200:
            raise RuntimeError(f"Supabase query failed ({response.status_code}): {response.text}")

        return response.json()