1- import os , sys
2- from typing import Any
3- from supabase import create_client , Client
4- from supabase .lib .client_options import ClientOptions
5- from abc import ABC , abstractmethod
6- import psycopg2 ,json
7- from psycopg2 .extras import RealDictCursor
8- from dotenv import load_dotenv
9-
10-
11- load_dotenv ()
12-
13- client_options = ClientOptions (postgrest_client_timeout = None )
14-
15-
16-
17- class SupabaseInterface ():
18-
19- _instance = None
20-
21- def __init__ (self ):
22- if not SupabaseInterface ._instance :
23-
24- # Load environment variables
25-
26- SUPABASE_URL = os .getenv ('SUPABASE_URL' )
27- SUPABASE_KEY = os .getenv ('SUPABASE_KEY' )
28- self .client : Client = create_client (SUPABASE_URL , SUPABASE_KEY )
29- SupabaseInterface ._instance = self
30- else :
31- SupabaseInterface ._instance = self ._instance
32-
33-
34-
35- @staticmethod
36- def get_instance ():
37- # Static method to retrieve the singleton instance
38- if not SupabaseInterface ._instance :
39- # If no instance exists, create a new one
40- SupabaseInterface ._instance = SupabaseInterface ()
41- return SupabaseInterface ._instance
42-
43-
44- def get_postgres_connection ():
45-
46- # Database configuration
47- DB_HOST = os .getenv ('POSTGRES_DB_HOST' )
48- DB_NAME = os .getenv ('POSTGRES_DB_NAME' )
49- DB_USER = os .getenv ('POSTGRES_DB_USER' )
50- DB_PASS = os .getenv ('POSTGRES_DB_PASS' )
51- conn = psycopg2 .connect (
52- host = DB_HOST ,
53- database = DB_NAME ,
54- user = DB_USER ,
55- password = DB_PASS
56- )
57- return conn
58-
59-
60-
61- def postgres_query (query ,params = None ):
62- conn = SupabaseInterface .get_postgres_connection ()
63-
64- cursor = conn .cursor (cursor_factory = RealDictCursor )
65-
66- # cursor = conn.cursor()
67- if not params :
68- cursor .execute (query )
69- else :
70- cursor .execute (query ,params )
71-
72- rows = cursor .fetchall ()
73- results_as_dicts = [dict (row ) for row in rows ]
74-
75- cursor .close ()
76- conn .close ()
77- return results_as_dicts
78-
79- def readAll (self , table ):
80- data = self .client .table (f"{ table } " ).select ("*" ).execute ()
81- return data .data
82-
83- def add_data (self , data ,table_name ):
84- data = self .client .table (table_name ).insert (data ).execute ()
85- return data .data
86-
87- def add_data_filter (self , data , table_name ):
88- # Construct the filter based on the provided column names and values
89- filter_data = {column : data [column ] for column in ['dmp_id' ,'issue_number' ,'owner' ]}
90-
91- # Check if the data already exists in the table based on the filter
92- existing_data = self .client .table (table_name ).select ("*" ).eq ('dmp_id' ,data ['dmp_id' ]).execute ()
93-
94- # If the data already exists, return without creating a new record
95- if existing_data .data :
96- return "Data already exists"
97-
98- # If the data doesn't exist, insert it into the table
99- new_data = self .client .table (table_name ).insert (data ).execute ()
100- return new_data .data
0 commit comments